在Java中写入文件。帮我写代码
我正在将一个文件路径传递给这个方法,该方法在txt文件中写入。但当我运行这个程序时,它并没有写满,我不知道我在哪里犯了错误在Java中写入文件。帮我写代码,java,file-io,filewriter,Java,File Io,Filewriter,我正在将一个文件路径传递给这个方法,该方法在txt文件中写入。但当我运行这个程序时,它并没有写满,我不知道我在哪里犯了错误 public void content(String s) { try { BufferedReader br=new BufferedReader(new FileReader(s)); try { String read=s; while((read = br.readLine()) != null) {
public void content(String s) {
try {
BufferedReader br=new BufferedReader(new FileReader(s));
try {
String read=s;
while((read = br.readLine()) != null) {
PrintWriter out = new PrintWriter(new FileWriter("e:\\OP.txt"));
out.write(read);
out.close();
}
} catch(Exception e) { }
} catch(Exception e) { }
}
在内部关闭PrintWriter,最后在环的外侧进行阻挡
finally {
out.close();
}
在内部关闭PrintWriter,最后在环的外侧进行阻挡
finally {
out.close();
}
您不应该每次都在循环中创建PrintWriter:
public void content(String s) {
BufferedReader br=new BufferedReader(new FileReader(s));
try {
PrintWriter out=new PrintWriter(new FileWriter("e:\\OP.txt"));
String read=null;
while((read=br.readLine())!=null) {
out.write(read);
}
} catch(Exception e) {
//do something meaningfull}
} finally {
out.close();
}
}
另外,正如其他人提到的添加finally块,不要静默捕获异常,并遵循Java编码约定。您不应该每次都在循环中创建PrintWriter:
public void content(String s) {
BufferedReader br=new BufferedReader(new FileReader(s));
try {
PrintWriter out=new PrintWriter(new FileWriter("e:\\OP.txt"));
String read=null;
while((read=br.readLine())!=null) {
out.write(read);
}
} catch(Exception e) {
//do something meaningfull}
} finally {
out.close();
}
}
另外,正如其他人提到的添加finally块,不要静默地捕获异常,并遵循Java编码约定。试试这个
public void content(String s) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(s));
PrintWriter pr = new PrintWriter(new File("e:\\OP.txt"))) {
for (String line; (line = br.readLine()) != null;) {
pr.println(line);
}
}
}
试试这个
public void content(String s) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(s));
PrintWriter pr = new PrintWriter(new File("e:\\OP.txt"))) {
for (String line; (line = br.readLine()) != null;) {
pr.println(line);
}
}
}
最好改用ApacheCommons IO 应该会成功
(除非您试图学习底层知识,或者确实知道为什么不能在这种情况下使用IOUtils。)最好改用Apache Commons IO 应该会成功
(除非您正在尝试学习底层内容,或者确实知道为什么不能在本例中使用IOUtils。)在完成之前关闭流。所以要么把它放进
<code>
finally {
out.close();
}
</code>
or see this simple example
<code>try {
String content = s;
File file = new File("/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
</code>
在完成之前关闭流。所以要么把它放进
<code>
finally {
out.close();
}
</code>
or see this simple example
<code>try {
String content = s;
File file = new File("/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
</code>
你不应该只是默默地捕捉异常。然后您可能会得到一条有意义的错误消息,输出文件上只写了一些字符。不是全部内容。有什么问题吗?你不应该只是默默地捕捉异常。然后您可能会得到一条有意义的错误消息,输出文件上只写了一些字符。不是全部内容。有什么问题吗?这个答案就是答案。美好的由于您的编码未正确对齐OP文件的格式。这个答案解决了我的问题:D这个答案就是答案。美好的由于您的编码未正确对齐OP文件的格式。这个答案解决了我的问题:D