【问题标题】:File I/O: Reading from one file and writing to another (Java)文件 I/O:从一个文件读取并写入另一个文件(Java)
【发布时间】:2023-04-05 22:44:01
【问题描述】:

我目前正在我的 cpe 课程的实验室中工作,我们必须创建一个简单的程序,它可以扫描 .txt 文件中的字符串并将它们打印到不同的 .txt 文件中。到目前为止,我已经制定了基本程序,但是尽管我拥有所有必要的文件,但我的异常仍然被抛出。谁能帮我调试一下?

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.");
    }
}
}

【问题讨论】:

  • 您的输入/输出文件没有扩展名?
  • 他已经说过它们是 .txt 文件......所以看起来这就是问题所在。对您计划在其上进行编程的任何计算机的一点建议:将文件浏览器设置为始终显示文件扩展名。
  • 别忘了关闭扫描仪和写入器。

标签: java file io


【解决方案1】:

您需要弄清楚它在哪里寻找"input" 文件。当您只指定"input" 时,它会在当前工作目录 中查找文件。使用 IDE 时,这个目录可能不是你想象的那样。

尝试以下方法:

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

查看它在哪里查找文件。

【讨论】:

  • 谢谢!!原来我的 .txt 文件在错误的文件夹中。所以它找到了它们,但现在它没有从我的 input.txt 写入我的 output.txt。 Output.txt 还是空的。
  • 那是因为你没有调用flush。我建议你在完成后调用printer.close()(它会为你刷新)。
  • 谢谢!!现在可以了!我关闭了扫描仪,但没有关闭打印机。
【解决方案2】:

也许你只是忘记了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.");
        }

【讨论】:

  • 他说他遇到了异常。
  • @aioobe 你的例外是什么?
  • 没有任何例外,Mike(发布问题的人)有。
  • 抛出异常是因为找不到文件,而不是因为打印机未能刷新其所有字节。
  • 我知道这是一个非常古老的答案/问题,但他确实需要一个 printer.close() 调用来完成对文件的写入
【解决方案3】:

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

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

【讨论】:

  • 我之前有 .txt 扩展名,但我仍然会抛出异常。我正在使用 eclipse 并且 .txt 文件与我的 .java 文件一起在 src 折叠中。
  • 我重新添加了扩展并将文件移动到正确的文件夹,但现在每次运行我的程序时,我的 output.txt 都会更新,但是当我打开它时,里面仍然没有任何内容。
【解决方案4】:

我们可以通过使用 FileInputStream 对象读取文件并使用 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);
      }

    }

 }

【讨论】:

    【解决方案5】:
    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();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-23
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多