PrintWriter 似乎没有写入文件,即使我正在关闭它
PrintWriter doesn't seem to be writing to file even though I'm closing it
我正在尝试使用 PrintWriter
打印到名为 output.txt
的预定 .txt
文件中。
我没有尝试将 File 和 FileWriter
都放在 PrintWriter
中,但没有成功。我也关了最奇怪的部分是 PrintWriter
在我的其他代码中的另一个类似用法似乎工作得很好(第二个)。
有人可以给我弹一下 pointers/hints 吗?
public static void main(String[] args) throws IOException{
if(args.length <4){
return;
}else{
String patternFileN = args[0];
String sequenceFileN = args[1];
int numOfSeqs = Integer.parseInt(args[2]);
int pattInd = Integer.parseInt(args[3]);
if(numOfSeqs <= 0){
return;
}
String pattern;
String[] txtSequence = new String[numOfSeqs];
PrintWriter outFile = null;
try{
String outFileN = "output.txt";
outFile = new PrintWriter(new File(outFileN));
for(int seqInd = 0; seqInd < numOfSeqs; seqInd++){
int offset = RKscanner.seqSearch(txtSequence[seqInd]);
if(offset < txtSequence[seqInd].length()){
//outFile.println(pattInd + " " + seqInd);
outFile.println("nigaaaa");
}
}
}catch(IOException e){
e.printStackTrace();
System.err.println("files couldn't be opened");
}finally{
outFile.close();
}
}
}
此代码运行良好:
public static void main(String[]args) throws IOException{
int letters = 4;
int pattLength, possNum; //number of possible sequences
try{
if(args.length <=1){
System.err.println("please specify output filename & lengths of patterns to be generated");
System.err.println("---try: java patternGen pattern.txt 5");
}else{
String fileName = args[0];
PrintWriter outFile = new PrintWriter(new FileWriter(fileName));
pattLength = Integer.parseInt(args[1]);
possNum = (int)Math.pow(letters, pattLength); //possible number of combinations without wildcards.
//Nucleotide[][] patternBuild = new Nucleotide[possNum][pattLength];//the built pattern to be used for consructor.
StringBuilder sb = new StringBuilder(pattLength);
for(int i = 0; i < possNum; i++){
sb.setLength(0);
StringBuilder alphToNuc = new StringBuilder(0);
String sbStore = sb.toString();
//...then string builders populated
//System.out.println("alphabet: "+sb);
outFile.println(alphToNuc);
System.out.println(alphToNuc);
}
outFile.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
已修复!
问题是我在 运行 这些 java 文件中使用 bash 脚本循环,所以当我打开并写入这些文件时,文件会被覆盖java 个文件。
很抱歉提出无关紧要的问题
我正在尝试使用 PrintWriter
打印到名为 output.txt
的预定 .txt
文件中。
我没有尝试将 File 和 FileWriter
都放在 PrintWriter
中,但没有成功。我也关了最奇怪的部分是 PrintWriter
在我的其他代码中的另一个类似用法似乎工作得很好(第二个)。
有人可以给我弹一下 pointers/hints 吗?
public static void main(String[] args) throws IOException{
if(args.length <4){
return;
}else{
String patternFileN = args[0];
String sequenceFileN = args[1];
int numOfSeqs = Integer.parseInt(args[2]);
int pattInd = Integer.parseInt(args[3]);
if(numOfSeqs <= 0){
return;
}
String pattern;
String[] txtSequence = new String[numOfSeqs];
PrintWriter outFile = null;
try{
String outFileN = "output.txt";
outFile = new PrintWriter(new File(outFileN));
for(int seqInd = 0; seqInd < numOfSeqs; seqInd++){
int offset = RKscanner.seqSearch(txtSequence[seqInd]);
if(offset < txtSequence[seqInd].length()){
//outFile.println(pattInd + " " + seqInd);
outFile.println("nigaaaa");
}
}
}catch(IOException e){
e.printStackTrace();
System.err.println("files couldn't be opened");
}finally{
outFile.close();
}
}
}
此代码运行良好:
public static void main(String[]args) throws IOException{
int letters = 4;
int pattLength, possNum; //number of possible sequences
try{
if(args.length <=1){
System.err.println("please specify output filename & lengths of patterns to be generated");
System.err.println("---try: java patternGen pattern.txt 5");
}else{
String fileName = args[0];
PrintWriter outFile = new PrintWriter(new FileWriter(fileName));
pattLength = Integer.parseInt(args[1]);
possNum = (int)Math.pow(letters, pattLength); //possible number of combinations without wildcards.
//Nucleotide[][] patternBuild = new Nucleotide[possNum][pattLength];//the built pattern to be used for consructor.
StringBuilder sb = new StringBuilder(pattLength);
for(int i = 0; i < possNum; i++){
sb.setLength(0);
StringBuilder alphToNuc = new StringBuilder(0);
String sbStore = sb.toString();
//...then string builders populated
//System.out.println("alphabet: "+sb);
outFile.println(alphToNuc);
System.out.println(alphToNuc);
}
outFile.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
已修复!
问题是我在 运行 这些 java 文件中使用 bash 脚本循环,所以当我打开并写入这些文件时,文件会被覆盖java 个文件。
很抱歉提出无关紧要的问题