How to avoid hardcoding text files in java

948 views Asked by At

I'm working on a project where 3 text files stored into a folder. I'm trying to avoid hardcoding the 3 paths into the source file.

That being said, I would like to run the program by typing at the prompt:java sourceCode folderName.

Please see below what I've achieved so far

:

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

    public class Indexes {

        //main method
        public static void main (String[] args)throws IOException {

            for (String MyFolder: args) {
                Scanner textFile =new Scanner(inputFile);

                //read the inputFile
                inputFile=textFile.nextLine();

                //two other objects
                File input =new File(inputFile);
                Scanner scan=new Scanner(input);

                int thesize=0;  
                int page=0;

                while (scan.hasNext()){

                    String readprt=scan.next();
                    //char[] c = s.toCharArray(); //conversion into character

                    char [] charArray=readprt.toCharArray();  //conversion of word of type string into type character
                    int size=charArray.length ; //length of word

                    thesize +=size;

                    if (thesize <=100){

                    }
                }
            }
        }
    }





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


public class Indexes {


    /*

    //creation of constructor
    public ReadFile (String inf){


    }

    public WriteFile (String outf){


    } */


    //main method
    public static void main (String[] args)throws IOException {

        for (String MyFolder: args) {

            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/huckfinn.txt";
            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/wap.txt";
            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/moby.txt";
            //System.out.println(MyFolder);



        //create objects for ("huckfinn.txt")

        //String inputFile="huckfinn.txt" ; //, outputFile="huckfinn_output.txt" ;
        String inputFile=MyFolder ;
        Scanner textFile =new Scanner(inputFile);

        //read the inputFile
        inputFile=textFile.nextLine();

        //write into  the outputFiles by defining an object for each of the files

        PrintWriter printer1 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/huckfinn_output.txt");
        PrintWriter printer2 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/wap_output.txt");
        PrintWriter printer3 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/moby_output.txt"); 

        //two other objects
        File input =new File(inputFile);
        Scanner scan=new Scanner(input);

        int thesize=0;  
        int page=0;



        while (scan.hasNext()){

            String readprt=scan.next();

            //char[] c = s.toCharArray(); //conversion into character


            char [] charArray=readprt.toCharArray();  //conversion of word of type string into type character
            int size=charArray.length ; //length of word




                        /*printer1.println(readprt+":"+" "+size); */
                        //printer1.println("Size of"+" "+readprt+":"+ " "+counter);


                        //printer2.println(readprt+":"+" "+size);
                        //printer2.println("Size of"+" "+readprt+":"+ " "+counter);


                        //printer3.println(readprt+":"+" "+size);
                        //printer3.println("Size of"+" "+readprt+":"+ " "+counter);


                                                 //read next word on the next line 
                    /* }
                }
            }  */

            //printer1.println(readprt);
            //printer2.println(readprt);
            //printer3.println(readprt); 


                                    // the government of the people by the people

                                    thesize +=size;

                                    if (thesize <=100){

                                        printer1.println(readprt+":"+" "+size);
                                        printer2.println(readprt+":"+" "+size);
                                        printer3.println(readprt+":"+" "+size);
                                        continue ;
                                    } else {

                                        page ++;

                                        printer1.println("character count <=100:"+ " "+(thesize-size));
                                        printer1.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        printer2.println("character count <=100:"+ " "+(thesize-size));
                                        printer2.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        printer3.println("character count <=100:"+ " "+(thesize-size));
                                        printer3.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        thesize=0;

                                    }



            }
        //close objects 
        printer1.close();
        printer2.close();
        printer3.close(); 




        //object to read in methods
        //Index ReadObj = new Index();



        }
    }
}
1

There are 1 answers

0
elirandav On

I am also new to JAVA, but I guess the question is very basic.

If you already know what is the path then you should create a .properties file for your project and read the path from this configuration file.

If you can know the path only on run time, the you should read it from args inserted in command line (or other input resource available in run time).

if the the prefix of the path is known but the suffix isn't then you should use the combination of the two approaches above and do the following:

String fullPath = prefix + suffix;