File I / O: reading from one file and writing to another (Java)

I am currently working in a lab in the cpe class, and we need to create a simple program that scans strings from a TXT file and prints them in another .txt file. So far I have the main program, but my exception continues to receive a throw, although I have all the necessary files. Can someone help me debug?

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.");
    }
}
}
+5
source share
5 answers

You need to find out where it is looking for the file "input". When you just specify "input", it searches for a file in the current working directory. When working with the IDE, this directory may not be what you think it is.

Try the following:

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

to see where he is looking for the file.

+4
source

Java I/O filetype ( ).

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

, 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.");
        }
+2
source

We can do this by reading the file using the FileInputStream object and writing to another file using the FileOutputStream object .

Here is a sample code

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

}
0
source

All Articles