我正在将一个文本文件加载到 EditText 中,但该文件仅被部分加载。我尝试了两个不同的文件并得到相同的结果。一个文件在第 35 行和第 37 行中途被截断。不知道为什么。
<com.mobilewebtoolkit.EditTextLineNumbers
android:id="@+id/ide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:gravity="left"
android:inputType="textMultiLine"
android:lineSpacingExtra="5dp"
android:textSize="15sp"
android:visibility="visible" >
代码:
private void openFile(final File aFile) {
String nullChk = et.getText().toString();
if (!changed || nullChk.matches("")) {
try {
currentFile = aFile;
getExt();
et.setText(new Scanner(currentFile).useDelimiter("\\Z").next());
changed = false;
exists = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Save first?");
alert.setMessage("(Will be saved in the current working directory)");
alert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String temptxt = et.getText().toString();
if (currentFile.exists()) {
try {
saveFile(currentFile.getPath(), temptxt);
currentFile = aFile;
getExt();
} catch (NullPointerException e) {
Log.i("NullPointer", currentFile.getName());
}
try {
et.setText(new Scanner(currentFile)
.useDelimiter("\\Z").next());
getExt();
if (extension.equals("txt")) {
Toast.makeText(MainActivity.this,
"Extension: " + extension,
Toast.LENGTH_LONG).show();
} else if (extension.equals("html")
|| extension.equals("htm")) {
Toast.makeText(MainActivity.this,
"Extension: " + extension,
Toast.LENGTH_LONG).show();
} else if (extension.equals("css")) {
Toast.makeText(MainActivity.this,
"Extension: " + extension,
Toast.LENGTH_LONG).show();
} else if (extension.equals("js")) {
Toast.makeText(MainActivity.this,
"Extension: " + extension,
Toast.LENGTH_LONG).show();
} else if (extension.equals("php")) {
Toast.makeText(MainActivity.this,
"Extension: " + extension,
Toast.LENGTH_LONG).show();
} else if (extension.equals("xml")) {
Toast.makeText(MainActivity.this,
"Extension: " + extension,
Toast.LENGTH_LONG).show();
}
changed = false;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
saveAs(null);
}
}
});
final File tempFile = aFile;
alert.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
try {
et.setText(new Scanner(tempFile).useDelimiter(
"\\Z").next());
changed = false;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
changed = false;
}
});
alert.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
changed = true;
dialog.cancel();
}
});
alert.show();
}
}