Note that there are some explanatory texts on larger screens.

plurals
  1. POJava reading text file
    text
    copied!<p>I have a question about txt file in the Java</p> <p>When I have to read the text file, i have to point out the path.</p> <p>However, txt file is in the same folder.</p> <p>What need to do is...</p> <p>testing readingoption filename.</p> <p>testing : class name readoption : option for reading file filename: file name that same folder.</p> <p>However, I don't want to use path to pointing out the file which means that I want to read the text file without using "C:/Users/myname/Desktop/myfolder/" in my code.</p> <p>does anybody know how to do it ?</p> <p>thanks.</p> <pre><code>public class testing{ private static boolean debug = true; public static void main(String args []) { if(args.length == 0) {// if you do nothing if(debug == true) { System.out.println(args); } System.err.println("Error: Missing Keywords"); return; } else if(args.length == 1) {// if you miss key word if(debug == true) { System.out.println(args); } System.err.println("Error: Missing filename"); return; } else {// if it is fine String pathes = "C:/Users/myname/Desktop/myfolder/";// dont want to use this part if(debug == true) { System.out.println("Everthing is fine"); System.out.println("Keyword :" + args[0]); System.out.println("File name :" + args[1]); } try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(pathes + "bob.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload