Java无法通过3种方法创建文件 [英] java cannot create file by 3 methods
问题描述
我的活动目录中有一个test.txt.我需要创建三种方法:
I have a test.txt in my active directory.I need to create three methods:
- 第一个必须创建一个输出文件并反转行的顺序.
- 第二个单词
- 第三个行和单词的顺序. test.txt包含输入.
- First one has to create an output file and reverse the order of the lines.
- Second the order of the words
- The third the order of the lines and the words. test.txt contains the input.
我开发了每种方法都可以单独工作,但是以某种方式当我同时调用这三个方法时似乎不起作用. 我究竟做错了什么? 这是我的主要任务:
I developed every method to work on its own but somehow when I call all three at the same time it doesnt seem to work. What am i doing wrong? This is my main:
import java.io.*;
public class DemoReverser {
public static void main (String [] args)
throws IOException, FileNotFoundException {
Reverser r = new Reverser(new File("test.txt"));
r.completeReverse(new File("out3.txt"));
r.reverseEachLine(new File("out2.txt"));
r.reverseLines(new File("out1.txt"));
} }
这是我的课程.
import java.util.*;
import java.io.*;
public class Reverser {
Scanner sc = null ;
Scanner sc2 = null;
//constructor takes input file and initialize scanner sc pointing at input
public Reverser(File file)throws FileNotFoundException, IOException{
sc = new Scanner (file);
}
//this method reverses the order of the lines in the output
//prints to output file specified in argument.
public void reverseLines(File outpr)throws FileNotFoundException, IOException{
List<String> wordsarraylist = new ArrayList<String>();
while(sc.hasNextLine()){
wordsarraylist.add(sc.nextLine());
}
Collections.reverse(wordsarraylist);
FileWriter writer = new FileWriter(outpr,true);
for(String str: wordsarraylist) {
writer.write(str+System.lineSeparator());
}
writer.flush();
writer.close();
}
//this method reverses the order of the words in each line of the input
//and prints it to output file specified in argument.
public void reverseEachLine(File outpr)throws FileNotFoundException, IOException{
while(sc.hasNextLine()){
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
ArrayList<String> wordsarraylist = new ArrayList<String>(Arrays.asList(words));
Collections.reverse(wordsarraylist);
FileWriter writer = new FileWriter(outpr,true);
for(String str: wordsarraylist) {
writer.write(str + " ");
}
writer.write(System.lineSeparator());
writer.flush();
writer.close();
}
}
//this methhod reverses the order of the words in each sentence of the input
//then writes it to output file specified in argument
//then uses the output file as input and reverses the order of the sentences
//then overwrites the ouput file with the result
//the output file will contain the input sentences with their words reversed
// and the order of sentences reversed.
public void completeReverse(File outpr) throws FileNotFoundException, IOException{
while(sc.hasNextLine()){
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
ArrayList<String> wordsarraylist2 = new ArrayList<String>(Arrays.asList(words));
Collections.reverse(wordsarraylist2);
FileWriter writer = new FileWriter(outpr,true);
for(String str: wordsarraylist2) {
writer.write(str + " ");
}
writer.write(System.lineSeparator());
writer.flush();
writer.close();
}
sc2 = new Scanner (outpr);
List<String> wordsarraylist = new ArrayList<String>();
while(sc2.hasNextLine()){
wordsarraylist.add(sc2.nextLine());
}
Collections.reverse(wordsarraylist);
PrintWriter erase = new PrintWriter(outpr);
erase.print("");
// erase.flush();
erase.close();
FileWriter writer = new FileWriter(outpr,true);
for(String str: wordsarraylist) {
writer.write(str+System.lineSeparator());
}
writer.flush();
writer.close();
}
}
当我运行程序时,已创建out1文件gest,这是我的第一种方法的输出文件,但为空.我没有得到用第二种方法创建的out2文件,并且out3很好. 我究竟做错了什么?错过了什么
When I run the program, out1 file gests created, which is the output file for my first method but it is empty. I don't get out2 file created by second method and out3 is fine. What am I doing wrong? What is missed
推荐答案
在所有三种方法中的writer.close();
之前添加writer.flush();
Add writer.flush();
before writer.close();
in all three methods
还有其他事情-扫描程序在构造函数中仅使用File初始化一次.它必须用其他方法重新初始化.
And other thing - Scanner is initialized with File only once in constructor. It has to be re-initialized in other methods.
sc =新的扫描仪(文件); //扫描仪应该在所有三种方法中都可用
sc = new Scanner (file); // the scanner should be available in all three methods
要捕获异常,请使用
try{
// your code
}catch(Exception err){
err.printStackTrace();
}
运行代码后,将生成output3.txt(第一个方法调用).由于到达文件末尾,以后的扫描程序不可用.
After running your code, output3.txt is generated (First method call). Later Scanner is not available since end of the file has been reached.
修复:接下来的两种方法应重新初始化扫描仪.
Fix : Scanner should be re-initialized for next two methods.
(使用您的聊天反馈更新答案)
1)由于您的限制,请创建三个扫描仪sc1,sc2和sc3.我建议使用正在处理的文件,以每种方法重新初始化扫描仪.
1) Create three scanners sc1,sc2 and sc3 due to your limitations. I would suggest to re-initialize the scanner in every method with the file being worked upon.
2)无需使用StringBuffer reverse()API即可以更简单的方式反转字符串(仅供学习)
2) String reversal in easier way without using StringBuffer reverse() API ( For learning purpose)
int length = str.length();
String reverse = "";
for ( int i = length - 1 ; i >= 0 ; i-- ){
reverse = reverse + str.charAt(i);
}
这篇关于Java无法通过3种方法创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!