0

I am facing file write issue ,Actually when i run this below code the while loop iterate infinite times.

package com.demo.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {

    public static void main(String[] args) {

        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream("C:/Users/s.swain/Desktop/loginissue.txt");
            out = new FileOutputStream("C:/Users/s.swain/Desktop/output.txt");
            int c = in.read();
            while (c != -1) {
                System.out.println(c);
                out.write(c);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Anyone can tell me how to write this file.

Thanks

Sitansu

3
  • 4
    I'd suggest using Apache Commons IO's IOUtils.copy(InputStream, OutputStream. Commented May 18, 2015 at 12:08
  • 1
    Although all answers just suggest to fix the bug in your code; I second the suggestion from Thomas: unless this is for homework/learning; you should not reinvent the wheel. There are libraries that do "file copy" for you; and most likely they are much better tested and will deal with many "cross-plattform" issues that you will not think of. Commented May 18, 2015 at 12:11
  • 1
    or Files.copy(Paths.get("pathToInputFile"), Paths.get("pathToOutputFile")); Commented May 18, 2015 at 12:12

6 Answers 6

3

This conditions remains true for eternity true because you never update c in the while-loop:

while (c != -1) {

Use in.read inside while-loop!

int c = in.read();
while (c != -1) {
    System.out.println(c);
    out.write(c);
    c = in.read();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I assume you meant: this condition remains true for eternity, once it becomes true.
3

You missed to read the next byte:

int c = in.read();
while (c != -1) {
    System.out.println(c);
    out.write(c);
    c = in.read();//this line added to read next byte
}

Or, you can simply use:

int c;
while (-1 != (c = in.read())) { /* condition with assignment */
    out.write(c);
}

Comments

2

You only read c once. Update your while-loop to

while (c != -1) {
    System.out.println(c);
    out.write(c);
    c = in.read();
}

Comments

1

Just do this

int c = 0;
 while ((c = in.read()) != -1) {
 System.out.println((char)c);
 out.write((byte)c);
 }

Comments

1

Try something like this:

        while(c != null){
            System.out.println(c);
            out.write(c);
            c = reader.readLine();
        }     

Comments

0
while (c != -1) {
                System.out.println(c);
                out.write(c);
            }

what do you think will happen here, if c != -1 ? You don't update the value of c, so it will cause an infinite loop.

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.