What's wrong in my code? It doesn't save the input I enter in console. It does create a new file though. But does't save anything.

public class FileTester1 {
static Scanner myScanner = new Scanner(System.in);
public static void main(String[] args) {
File inputFile = new File("C:\\Users\\T-Bone\\Desktop\\fileIn.txt");
File outputFile = new File("C:\\Users\\T-Bone\\Desktop\\fileOutn.txt");
try {
FileReader fileReader = new FileReader(inputFile);
try {
FileWriter fileWriter = new FileWriter(outputFile);
System.out.println("enter");
while (myScanner.hasNext()) {
String tempString = myScanner.nextLine();
fileWriter.write(tempString);
}
fileReader.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("file not found");
        }
}
}

asked 28 Jul '17, 10:01

Atik's gravatar image

Atik
111


The problem is in your myScanner.hasNext() , since you're taking input from command line, there's no way to tell the program to catch an EOF , and pressing ctrl+c will terminate the process. Instead of looking for EOF, add a condition that upon receiving a certain input, scanner will return control to the next block. Also avoid unnecessary try catch blocks, they can be fit into one.

Your same code can be written like this and it works just fine.

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

    public class FileTester1 {
        static Scanner myScanner = new Scanner(System.in);
        public static void main(String[] args) {
            File outputFile = new File("//Users//shawon//Desktop//2.txt");
            try {
                FileWriter fileWriter = new FileWriter(outputFile);
                System.out.println("enter: ");

                String tempString;

                // input will terminate when user enters EOF
                while ((tempString = myScanner.nextLine()).equals("EOF") == false) {
                    fileWriter.write(tempString);
                }
                fileWriter.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("file not found");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
}
permanent link

answered 29 Jul '17, 12:28

ShawonAshraf's gravatar image

ShawonAshraf
2345

edited 29 Jul '17, 12:32

Why there's two catch block?If I remove the last catch block it's giving me error.

(29 Jul '17, 12:46) Atik

Well it catches two types of exceptions , that's why!

(29 Jul '17, 13:20) ShawonAshraf
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×205

question asked: 28 Jul '17, 10:01

question was seen: 242 times

last updated: 29 Jul '17, 13:20