2012-05-14 195 views
5

我目前正在研究我的cpe类中的实验,我们必须创建一个简单的程序,它可以从.txt文件扫描字符串并打印他们到一个不同的.txt文件。到目前为止,我已经绘制出了基本程序,但是我的例外一直在抛出,尽管我拥有所有必需的文件。任何人都可以帮我调试吗?文件I/O:从一个文件读取并写入另一个文件(Java)

import java.io.*; 
import java.util.*; 

public class FileIO { 

public static void main(String args[]) {   
    try { 
     File input = new File("input"); 
     File output = new File("output"); 
     Scanner sc = new Scanner(input); 
     PrintWriter printer = new PrintWriter(output); 
     while(sc.hasNextLine()) { 
      String s = sc.nextLine(); 
      printer.write(s); 
     } 
    } 
    catch(FileNotFoundException e) { 
     System.err.println("File not found. Please scan in new file."); 
    } 
} 
} 
+1

你的输入/输出文件不具有扩展名? – apnorton

+1

他已经说过他们是.txt文件了...所以看起来就是这个问题。对计划进行编程的计算机提供一些建议:将文件浏览器设置为始终显示文件扩展名。 – Tharwen

+0

不要忘记关闭扫描仪和写入器。 –

回答

4

您需要计算出"input"文件的外观。当您只指定"input"时,它会在当前工作目录中查找该文件。使用IDE时,该目录可能不是您想象的那样。

尝试以下操作:

System.out.println(new File("input").getAbsolutePath()); 

,看看它会查找该文件。

+0

谢谢!事实证明,我的.txt文件在错误的文件夹中。所以它找到了它们,但现在它不会将我的input.txt写到output.txt。 Output.txt仍然是空的。 – Mike

+0

*这是*,因为你不会调用flush。我建议你在完成后调用printer.close()(这会为你刷新它)。 – aioobe

+0

谢谢!它现在有效!我关闭了扫描仪,但没有打印机。 – Mike

3

使用Java I/O访问文件时,必须包含文件的文件类型扩展名(如果存在)。

File input = new File("input.txt"); 
    File output = new File("output.txt"); 
+0

之前我有.txt扩展名,我仍然会抛出异常。我正在使用eclipse,并且.txt文件与我的.java文件一起位于src中。 – Mike

+0

我添加了扩展,并将文件移动到正确的文件夹中,但是现在每次运行我的程序时,我的output.txt都会更新,但是当我打开它时,仍然没有任何内容。 – Mike

2

可能你只是使用的FileInputStream对象读取文件忘记flush()

 try { 
      File input = new File("input"); 
      File output = new File("output"); 
      Scanner sc = new Scanner(input); 
      PrintWriter printer = new PrintWriter(output); 
      while (sc.hasNextLine()) { 
       String s = sc.nextLine(); 
       printer.write(s); 
      } 
      **printer.flush();** 
     } 
     catch (FileNotFoundException e) { 
      System.err.println("File not found. Please scan in new file."); 
     } 
+0

他说他得到一个例外。 – aioobe

+0

@aioobe你的例外是什么? – isvforall

+0

*我*没有任何例外,* Mike *(发布该问题的人)拥有。 – aioobe

0

我们可以做到这一点,并写入使用FileOutputStream中对象另一个文件。

下面是示例代码

package java_io_examples; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.Vector; 

public class Filetest { 

    public static void main(String[] args) { 

    try { 

      FileInputStream fin = new FileInputStream("D:\\testout.txt"); 

      int i = 0; 
      String s = ""; 

      while((i=fin.read())!=-1) { 

       s = s + String.valueOf((char)i); 

      } 

      FileOutputStream fout = new 
      FileOutputStream("D:\\newtestout1.txt"); 
      byte[] b = s.getBytes(); 

      fout.write(b); 
      fout.close(); 

      System.out.println("Done reading and writing!!"); 

     } catch(Exception e){ 
     System.out.println(e); 
     } 

    } 

} 
0
public void readwrite() throws IOException 
{ 
    // Reading data from file 
    File f1=new File("D:/read.txt"); 
    FileReader fr=new FileReader(f1); 
    BufferedReader br=new BufferedReader(fr); 

    String s = br.readLine(); 

    // Writing data 
    File f2=new File("D:/write.txt"); 
    FileWriter fw=new FileWriter(f2); 
    BufferedWriter bw=new BufferedWriter(fw); 
    while(s!=null) 
    { 
     bw.write(s); 
     bw.newLine(); 
     System.out.println(s); 
     s=br.readLine(); 

    } 
    bw.flush(); 
    bw.close(); 

} 
相关问题