如何避免在java中硬编码文本文件

如何避免在java中硬编码文本文件,java,hard-coding,Java,Hard Coding,我正在做一个项目,其中3个文本文件存储在一个文件夹中。我试图避免将这3条路径硬编码到源文件中 也就是说,我希望通过在提示符处键入:java sourceCode folderName来运行该程序 请看下面我到目前为止所取得的成就 : import java.util.Scanner ; import java.io.*; public class Indexes { //main method public static void m

我正在做一个项目,其中3个文本文件存储在一个文件夹中。我试图避免将这3条路径硬编码到源文件中

也就是说,我希望通过在提示符处键入:java sourceCode folderName来运行该程序

请看下面我到目前为止所取得的成就

:

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



        }
    }
}
:
导入java.util.Scanner;
导入java.io.*;
公共类索引{
//主要方法
公共静态void main(字符串[]args)引发IOException{
用于(字符串MyFolder:args){
扫描仪文本文件=新扫描仪(inputFile);
//读取输入文件
inputFile=textFile.nextLine();
//另外两个物体
文件输入=新文件(inputFile);
扫描仪扫描=新扫描仪(输入);
int thesize=0;
int page=0;
while(scan.hasNext()){
String readprt=scan.next();
//char[]c=s.toCharArray();//转换为字符
char[]charArray=readprt.toCharArray();//将字符串类型的单词转换为字符类型
int size=charArray.length;//字长
大小+=大小;

if(thesize我也是JAVA新手,但我想这个问题很基本

如果您已经知道什么是
路径
,那么您应该为项目创建一个
.properties
文件,并从该配置文件中读取路径

如果只能在运行时知道
路径
,则应该从命令行中插入的
args
读取路径(或运行时可用的其他输入资源)

如果路径
的前缀已知,但后缀不已知,则应使用上述两种方法的组合,并执行以下操作:

String fullPath = prefix + suffix;

您的问题尚不清楚。您是只想通过命令行参数输入文件名,还是想在程序启动后提示用户?您的命令行参数应该接受多少路径名?听起来好像您要传递一个文件夹位置并读取给定文件夹/目录中的所有文件?是的,我想要传递文件夹名称并读取给定文件夹中的所有文件。现在我不想将文件夹路径插入代码。这将允许任何拥有代码的人读取给定文件夹名称(比如folderName)中的文件。看看答案,实际上我不想在源代码中嵌入包含所有要读取文件的文件夹的路径。假设我给你我要构建的代码,你在你的计算机上创建一个名为folderName的文件夹,其中包含3个或更多或更少的文件,你应该能够读取所有文件和cre为每个文件创建一个输出。