0

我正在开发一个项目,其中 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();



        }
    }
}
4

1 回答 1

0

我也是 JAVA 的新手,但我想这个问题非常基础。

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

如果您知道path唯一的运行时,您应该从args插入命令行(或运行时可用的其他输入资源)中读取它。

如果 the 的前缀path已知但后缀不知道,那么您应该使用上述两种方法的组合并执行以下操作:

String fullPath = prefix + suffix;
于 2017-02-03T17:56:14.827 回答