2012-05-19 127 views
3

这是一个代码片段,但基本上我想要做的是从名为'listings.txt'的文件读取并写入名为'overview.txt'的文件。我想从'listings.txt'中取出信息并将它们放入'overview.txt'(我将在稍后介绍其余部分)。Java读/写到文件 - BufferedReader BufferedWriter

文件'overview.txt'已创建,似乎循环遍历文件'listings.txt'并写入'overview.txt'。但是,一旦我打开文件'overview.txt'它是空的。
有人可以快速浏览我的代码并发现错误的东西吗?

package yesOverview; 

import java.io.BufferedReader; 
import java.io.*; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 


public class yesOverview { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String strInput = "foo.bar"; 
     System.out.print("Please enter the listings file (the full path to the file): "); 
     strInput = input.next(); 

     //This makes sure that the inputed file is listings.txt as required for KET1 task 2 
     while (strInput.contains("listings.txt") == false) { 
      System.out.print("Incorrect file. Please enter listings file(the full path to the file): "); 
      strInput = input.next(); 
     } 

     infos(strInput); 
     input.close(); 
    } 

    public static void infos(String strInput) { 
     Scanner input2 = new Scanner(System.in); 
     System.out.print("Please enter the overview.txt file (the full path to the file): "); 
     String strInput2 = "foo.bar"; 
     strInput2 = input2.next(); 

     //This also makes sure that the overview.txt file is provided. 
     while (strInput2.contains("overview.txt") == false) { 
      System.out.print("Incorrect file. Please enter overview file(the full path to the file): "); 
      strInput2 = input2.next(); 
     } 

     //Creates the file f then places it in the specified directory. 
     File f = new File(strInput2); 

     try { 
      //Creates a printerwriter out that writes to the output file. 
      PrintWriter out = new PrintWriter(strInput2); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(KETTask2Overview.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     //String that holds the value of the next line. 
     String inputLine = ""; 

     //Creates the Buffered file reader/writer. 
     try { 
      BufferedReader in = new BufferedReader(new FileReader(strInput)); 
      FileWriter fstream = new FileWriter(strInput2); 
      BufferedWriter out = new BufferedWriter(fstream); 
      while (in.readLine() != null) { 
       out.write(in.read()); 
      } 
      in.close(); 
     } catch (IOException e) { 
      System.err.println("Error: " + e.getMessage()); 
     } 

    } 
} 
+6

你永远不会关门。 – bmargulies

+2

@bmargulies是正确的 - 你必须关闭。最好的做法是将close调用放在'finally {}'块中。 – user949300

+0

@ user949300或者在Java 7中使用[try-with-resources](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)语句。 – Jeffrey

回答

6

试试这个

关闭的BufferedWriter流(即out.close())

尝试使用nextLine(),而不是下一个(),为下一个()只需要在一个单一的字,但对于一个完整的行使用nextLine(),虽然这似乎没有问题在这里。

当我有读取和写入文件我该怎么办,我通常遵循以下步骤

从文件

File f = new File("my.txt"); 
FileReader fr = new FileReader(f); 
BufferedReader br = new BufferedReader(fr); 

String s = null; 

while ((br.readLine())!=null) { 

// Do whatever u want to do with the content of the file,eg print it on console using SysOut...etc 

} 

br.close(); 

阅读写入文件:

Boolean isDone = true; 
Scanner scan = new Scanner(System.in); 
File f = new File("my.txt"); 
FileWriter fr = new FileWriter(f); 
BufferedWriter br = new BufferedWriter(fr); 

while (b) { 

    if (!b) { 

br.write(new Scanner(System.in).nextLine()); 

} 


} 
+0

整个问题是我没有把它关闭,虽然你提供的这个代码也是非常有用的。感谢您的帮助! – RedHatcc

+2

你如何摆脱while(b)循环,为什么当b在其内部永远不会是虚假的时候,你为什么要测试!b?这没有任何意义。 – EJP

+0

是否需要此行?: * Scanner scan = new Scanner(System.in); * –

1

你没有结束。 writeList方法的finally块将清除并关闭BufferedWriter。

finally { 
    if (out != null) { 
     System.out.println("Closing BufferedWriter"); 
     out.close(); 
    } else { 
     System.out.println("BufferedWriter not open"); 
    } 
} 
4
public static long copy (Reader input, Writer output) throws IOException { 

    char[] buffer = new char[8192]; 
    long count = 0; 
    int n; 
    while ((n = input.read(buffer)) != -1) { 
     output.write(buffer, 0, n); 
     count += n; 
    } 
    return count; 
} 

使用示例:

copy(reader, new FileWriter(file)); 
相关问题