0

Code:

InputStream inputStream = System.in;
InputReader in = new InputReader(inputStream);

How could I modify this code to take a file from a fixed location?

Equivalent scanner example:

Scanner in = new Scanner(new InputStreamReader(System.in));
Scanner in = new Scanner(new FileReader("C:\\Users\\Max\\Desktop\\yes.txt"));
Community
  • 1
  • 1
user6250837
  • 448
  • 2
  • 15
  • do you want to read a file? – Md. Nasir Uddin Bhuiyan Jul 22 '16 at 08:49
  • I'm not sure what you're actually asking for but if you don't want to hard code the path (which is generally not advisable) you could use a property or argument to `main(...)` which can be set in your run configuration in Eclipse. – Thomas Jul 22 '16 at 08:50
  • yes!! i want to read a file – user6250837 Jul 22 '16 at 08:50
  • @Thomas can you explain how to that in eclipse – user6250837 Jul 22 '16 at 08:50
  • In Eclipse use `Run > Run Configurations...` and create a configuration for the type of application you want to run (most likely a "Java Application"). Then enter the path in the "Arguments" tab and finally run the configuration you just created (either via the "Run" button in the dialog or the run button in the menu bar which looks like a green "play" button). – Thomas Jul 22 '16 at 08:55
  • @Thomas Program or VM Arguments ? – user6250837 Jul 22 '16 at 09:00
  • And what is wrong with FileReader? – ead Jul 22 '16 at 09:00
  • You might want to read the documentation on that. Basically it depends on how you want to get the value. VM arguments would be properties (i.e. what you'd pass with `-Dxxx`) while program arguments would be what you pass to `main()`. – Thomas Jul 22 '16 at 09:06
  • Welcome to Stack Overflow! I edited your question as far as I could guess your problem. However, add code and description so that more people with knowledge of the subject will see it. Please edit in the specific error-message you're encountering in case that's necessary to identify the specific problem. Good Luck! – Enamul Hassan Jul 24 '16 at 10:48
  • Could you please mark as accepted the answer that was the most helpful? It shows other users that your problem was solved, and which answer to check out first. – m69 ''snarky and unwelcoming'' Aug 10 '16 at 01:21

2 Answers2

2

if you only want the whole text in one string than you can use the FileInputStream

FileInputStream inputStream = new FileInputStream("foo.txt");
try {
    String everything = IOUtils.toString(inputStream);
} finally {
    inputStream.close();
}

if you want to read the file line bye line use the BufferdReader in Connection with StringBuilder

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

while (line != null) {
    sb.append(line);
    sb.append(System.lineSeparator());
    line = br.readLine();
}
String everything = sb.toString();

}finally {
    br.close();
}

Note:

The examples are taken from @Knubo and the whole answer and further Information can be found here
Community
  • 1
  • 1
mayha
  • 593
  • 5
  • 15
0

To read file using Scanner class.

Try this.

Scanner input=null;
try{
    input=new Scanner(new File("your_file_path\yes.txt"));
} catch(Exception ex){
    System.out.println(ex.getMessage());
}

while(input.hasNext()){
    System.out.println(input.next());
}

input.close();
Md. Nasir Uddin Bhuiyan
  • 1,568
  • 1
  • 13
  • 22