簡體   English   中英

文件I / O:從一個文件讀取並寫入另一個文件(Java)

[英]File I/O: Reading from one file and writing to another (Java)

我目前正在我的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.");
    }
}
}

您需要弄清楚它在哪里查找"input"文件。 當您只指定"input"它會在當前工作目錄中查找該文件。 使用IDE時,此目錄可能不是您認為的那樣。

請嘗試以下方法:

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

查看它在哪里查找文件。

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

使用Java I / O訪問文件時,必須包含文件的文件類型擴展名(如果存在)。

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

我們可以通過使用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);
      }

    }

 }
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();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM