I'm writing this notepad app where the user types in what they want into an EditText
, it will write
that to a file, and then later they can read
it in a TextView
.
Here is my XML
code for the EditText I'm using:
<EditText
android:id="@+id/txtWrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnLogOut"
android:layout_marginTop="42dp"
android:layout_toLeftOf="@+id/btnAppendd"
android:ems="5"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine"
android:maxLength="2147483646" />
The max length
line was my attempt to fix it, seeing as I thought that would be plenty for the user to input
. However, it doesn't work.
When I display the length
of the file, it says that it is 1024, which makes sense, considering how I input
data from my files:
try {
char[] inputBuffer = new char[1024];
FileInputStream fIn = openFileInput("txt");
InputStreamReader isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
String data = new String(inputBuffer);
isr.close(); fIn.close();
display.setText(data + '\n' + '\n' + data.length()); // data + 1024
}catch(Exception e){}
That is how I input all of my files (Googled), and so I assume the new char[1024]
is the reason the max length
of the file can only be 1024.
Does anyone know another way of inputting files with limitless length? I don't even know why that has to be new char[1024]
.
Here is how I write
to files, which is nice because it's short code:
FileOutputStream fos = openFileOutput("txt", Context.MODE_PRIVATE);
fos.write(...);
fos.close();
Here is my full write
method:
public void write()
{
Button write = (Button)findViewById(R.id.btnWrite);
write.setOnClickListener (new View.OnClickListener()
{
public void onClick(View v)
{
EditText writeText = (EditText)findViewById(R.id.txtWrite);
try {
FileOutputStream fos = openFileOutput("txt", Context.MODE_PRIVATE);
fos.write(writeText.getText().toString().getBytes());
fos.close();
} catch(Exception e) {}
}
});
}