2

I have a question about txt file in the Java

When I have to read the text file, i have to point out the path.

However, txt file is in the same folder.

What need to do is...

testing readingoption filename.

testing : class name readoption : option for reading file filename: file name that same folder.

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.

does anybody know how to do it ?

thanks.

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());
                          }
                   }


    }
}
1
  • Why is only the name passed in args? Why not pass the entire path? What is the content of the file? Is it application resources? Does the information need to be user editable? Commented Sep 10, 2012 at 1:49

4 Answers 4

1

Change this line String pathes = "C:/Users/myname/Desktop/myfolder/"; to:

 String pathes = args[1];

and this line FileInputStream fstream = new FileInputStream(pathes + "bob.txt"); to:

FileInputStream fstream = new FileInputStream(pathes);
Sign up to request clarification or add additional context in comments.

2 Comments

can we do without using "/myfolder/bob.txt"? I mean I would like to use bob.txt only. I put the bob.txt file in the same folder
@DcRedwing: huh? I'm telling you to replace any reference to myfolder or bob.txt.
0

If you put text file into i.e. "myfolder" in your java project your path should be like this:

String pathes = "/myfolder/bob.txt";

FileInputStream fstream = new FileInputStream(pathes);

4 Comments

can we do without using "/myfolder/bob.txt"? I mean I would like to use bob.txt only..
Well...you can. If you just create a bob.txt file in your project (without putting file into a folder)
i put bob.txt file in the src folder, so jave file and txt file is in the same folder now..
Ok, but your path will have to be "src/bob.txt"
0

Load the path of the file from a .properties file (use the java.util.Properties class), or pass it as a parameter from commandline (in your main String[] argument).

Either way, your code that processes the file must not do it, it must be done in the outside. Your processing code receives the file path from the method calling it, so you do not need to change if you decide to use another method (v.g., a GUI).

Comments

0

You can use "." for the actual Path and add the system dependend file seperator like:

FileInputStream fstream = new FileInputStream("."+System.getProperty("file.separator")+"bob.txt");

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.