我正在开展一个项目,其中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();
}
}
}
答案 0 :(得分:0)
我也是JAVA的新手,但我想这个问题非常基础。
如果您已经知道path
是什么,那么您应该为项目创建一个.properties
文件,并从该配置文件中读取路径。
如果只能在运行时知道path
,则应从命令行中插入的args
(或运行时可用的其他输入资源)中读取它。
如果已知path
的前缀但后缀不是,那么您应该使用上述两种方法的组合并执行以下操作:
String fullPath = prefix + suffix;