![](/img/trans.png)
[英]data/data/package/files/myfile.txt is not copied to apk file
[英]nothing is present in myfile.txt
为什么myfile.txt为空? 我尝试将内容附加到myfile.txt,但是当我打开它时仍然为空。 正在创建文件。
import java.io.*;
public class NewClass1 {
@SuppressWarnings("empty-statement")
public static void main(String[] args) throws IOException{
File inputFile = new File("input.txt");
FileReader in1=null;
in1 = new FileReader("input.txt");
char c;
int count=0;
int r;
String s="";
File file;
file = new File("myfile.txt");
file.createNewFile();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
while((r=in1.read())!=-1) {
c = (char)r;
s=s+c;
if (c == '1' ) {
count++;
}
if (c == '\n') {
if (count>1) {
out.print(s);
}
s = "";
count=0;
}
}
}
}
您必须刷新并关闭输出文件,才能将最后一个充满字符的缓冲区实际保存到磁盘。
out.flush();
out.close();
BufferedWriter的目的是将这些字符保存在内存中的缓冲区中,直到刷新缓冲区为止。
调用'close'也应该刷新,因此调用它也将导致缓冲区也被写入。
还有其他可能的原因:我假设您已检查input.txt是否为空,并且其中至少包含一个换行符(\\ n)。 该算法仅在看到换行符时写入,而如果不存在换行符,则不写入任何内容。
最后,我不建议使用“ FileWriter”,因为字符编码取决于您的操作环境,该环境可能会发生变化。 最好使用OutputStreamWriter和FileOutputStream指定'UTF-8'(或其他一些特定的字符编码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.