1

My data is not getting transferred to the output file , I always get an Exception.

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

class TransferData {
    public static void main(String[] args) {
       String path1="E:\\IO\\Input.txt";
       String path2="E:\\IO\\Output.txt";
       int data;
       System.out.println("Transfering started...");
       try {
           FileInputStream  fis=new FileInputStream(path1);
           FileOutputStream fos=new FileOutputStream(path2);
           while((data=fis.read())!=-1) {
               fos.write(data);
           }
       }
       catch(Exception e) {
           System.out.println("exception caught!");
       }
       System.out.println("Completed...");
    }
}

How do I transfer data to output file ?

3
  • 2
    What Exception? Can you provide a StackTrace please? Commented May 1, 2015 at 14:33
  • 1
    Exception caught ! message appears Commented May 1, 2015 at 14:41
  • Add e.printStackTrace() to the catch block and post the message that appears. Commented May 1, 2015 at 15:04

4 Answers 4

2

Tested this code on my local machine it is works without exceptions.

  1. Check is file E:/IO/Input.txt exists.
  2. IS Directory E:/IO is writeable for your user (If file E:/IO/Output.txt already exists check is it writeable and not opened in another programm)

By code: It is good practice to close FIS and FOS after programm finished execution.

public class TransferData {
   public static void main(String[] args) {
      String path1 = "E:\\IO\\Input.txt";
      String path2 = "E:\\IO\\Output.txt";
      int data;
      System.out.println("Transfering started...");
      FileInputStream fis = null;
      FileOutputStream fos = null;
      try {
         fis = new FileInputStream(path1);
         fos = new FileOutputStream(path2);
         while ((data = fis.read()) != -1) {
            fos.write(data);
         }
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         try {
            if (fis != null) {
               fis.close();
            }
            if (fos != null) {
               fos.close();
            }
         } catch (IOException e) {
            e.printStackTrace();
         }

      }
      System.out.println("Completed...");
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you replace System.out.println("exception caught!"); with e.printStackTrace(); then you will get a much more useful error message.

If you then post that error message here, people will be able to help you much more easily.


It could be the case that the program cannot find the file you're trying to read.

Comments

1

I highly suggest to use e.printStackTrace() as the others suggested.

One possible problem might be the filesystem permissions or the file you are trying to read from being not existent.

You might also want to use a "try with resources" to simplify your code. Your code is missing a close statement for your Streams too.

All together your code would look something like this:

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

class TransferData {
    public static void main(String[] args) {
        String path1="E:\\IO\\Input.txt";
        String path2="E:\\IO\\Output.txt";
        int data;
        System.out.println("Transfering started...");
        try (
            FileInputStream  fis=new FileInputStream(path1);
            FileOutputStream fos=new FileOutputStream(path2)
        ) {
            while((data=fis.read())!=-1) {
                fos.write(data);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

One last thing, if you post your code on StackOverflow, please do not mix different formatting styles (e.g. { in the same line as an if and sometimes in the next) and try to have the code well formatted from the beginning.

Comments

0

Add e.printStackTrace() to your catch block, and post the data printed in your console here, people will be able to help you better.


The most likely cause of the exception getting thrown is that the system is not able to find the file "E:\\IO\\Input.txt" or "E:\\IO\\Output.txt" make sure that the file's are there and Output.txt is not set to read only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.