Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java无法通过3种方法创建文件_Java_Output_Reverse - Fatal编程技术网

java无法通过3种方法创建文件

java无法通过3种方法创建文件,java,output,reverse,Java,Output,Reverse,我的active directory中有一个test.txt。我需要创建三个方法: 首先,必须创建一个输出文件并颠倒行的顺序 其次是单词的顺序 第三个是行和词的顺序。 test.txt包含输入 我开发了每一种方法都可以独立工作,但不知何故,当我同时调用这三种方法时,它似乎不起作用。 我做错了什么? 这是我的主要观点: import java.io.*; public class DemoReverser { public static void main (String [] args

我的active directory中有一个test.txt。我需要创建三个方法:

  • 首先,必须创建一个输出文件并颠倒行的顺序
  • 其次是单词的顺序
  • 第三个是行和词的顺序。 test.txt包含输入
  • 我开发了每一种方法都可以独立工作,但不知何故,当我同时调用这三种方法时,它似乎不起作用。 我做错了什么? 这是我的主要观点:

     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();
    
    
    
    
    
    }
    
    import java.util.*;
    导入java.io.*;
    公共类反向器{
    扫描仪sc=空;
    扫描仪sc2=空;
    //构造函数获取输入文件并初始化指向输入的扫描程序sc
    公共反向器(文件)抛出FileNotFoundException、IOException{
    sc=新扫描仪(文件);
    }
    //此方法反转输出中行的顺序
    //打印到参数中指定的输出文件。
    公共void reverseLines(文件输出)抛出FileNotFoundException、IOException{
    List wordsarraylist=新建ArrayList();
    while(sc.hasNextLine()){
    添加(sc.nextLine());
    }
    收藏。反向(单词列表);
    FileWriter writer=新的FileWriter(输出,true);
    for(字符串str:wordsarraylist){
    writer.write(str+System.lineSeparator());
    }
    writer.flush();
    writer.close();
    }
    //此方法反转输入每行中的单词顺序
    //并将其打印到参数中指定的输出文件中。
    public void reverseEachLine(文件输出)抛出FileNotFoundException、IOException{
    while(sc.hasNextLine()){
    字符串语句=sc.nextLine();
    字符串[]单词=句子。拆分(“”);
    ArrayList wordsarraylist=新的ArrayList(Arrays.asList(words));
    收藏。反向(单词列表);
    FileWriter writer=新的FileWriter(输出,true);
    for(字符串str:wordsarraylist){
    writer.write(str+“”);
    }
    writer.write(System.lineSeparator());
    writer.flush();
    writer.close();
    }
    }
    //这种方法颠倒了输入的每个句子中单词的顺序
    //然后将其写入参数中指定的输出文件
    //然后使用输出文件作为输入,并颠倒句子的顺序
    //然后用结果覆盖输出文件
    //输出文件将包含输入句子,其单词颠倒
    //句子的顺序颠倒了。
    public void completeReverse(文件输出)抛出FileNotFoundException、IOException{
    while(sc.hasNextLine()){
    字符串语句=sc.nextLine();
    字符串[]单词=句子。拆分(“”);
    ArrayList wordsarraylist2=新的ArrayList(Arrays.asList(words));
    收藏。反面(单词raylist2);
    FileWriter writer=新的FileWriter(输出,true);
    for(字符串str:wordsarraylist2){
    writer.write(str+“”);
    }
    writer.write(System.lineSeparator());
    writer.flush();
    writer.close();
    }
    sc2=新扫描仪(输出);
    List wordsarraylist=新建ArrayList();
    while(sc2.hasNextLine()){
    wordsarraylist.add(sc2.nextLine());
    }
    收藏。反向(单词列表);
    PrintWriter擦除=新的PrintWriter(输出);
    删除。打印(“”);
    //擦除。刷新();
    擦除。关闭();
    FileWriter writer=新的FileWriter(输出,true);
    for(字符串str:wordsarraylist){
    writer.write(str+System.lineSeparator());
    }
    writer.flush();
    writer.close();
    }
    
    }

    当我运行程序时,会创建out1文件gests,这是我的第一个方法的输出文件,但它是空的。我没有得到由第二个方法创建的out2文件,out3也可以。 我做错了什么?缺少什么

    添加
    writer.flush()
    before
    writer.close()在所有三种方法中

    还有一件事——扫描器在构造函数中仅用文件初始化一次。它必须用其他方法重新初始化

    sc=新扫描仪(文件);//扫描仪应具有所有三种方法

    要捕获异常,请使用

    try{
       // your code
    }catch(Exception err){
       err.printStackTrace();
    } 
    
    运行代码后,将生成output3.txt(第一个方法调用)。由于已到达文件末尾,后期扫描仪不可用

    修复:应该为接下来的两种方法重新初始化扫描仪

    编辑:(使用聊天反馈更新答案)

    1) 由于您的限制,创建三个扫描仪sc1、sc2和sc3。我建议在处理文件的每个方法中重新初始化扫描仪

    2) 无需使用StringBuffer reverse()API即可轻松实现字符串反转(用于学习)


    如果您在当前尝试的位置没有写入权限,则有可能。所以测试时会出现错误

    然而,您在代码中犯了一些错误
    reverseEachLine
    方法不起作用,您不应该浪费代码来创建
    completeReverse
    方法。请注意以下事项

    • 在需要时构造
      扫描仪
    • 请记住关闭
      扫描仪
    • 关闭
      扫描仪后写入处理过的文件
    • 如果不需要在
      文件写入程序中删除append
    • 记住刷新
      FileWriter
    • 确定方法之间的相似性和关系(完全反转是线条反转和单词反转的组合)
    公共类反向器{

    private File inputFile;
    
    public MyReverser(File file) {
        this.inputFile = file;
    }
    
    public void reverseLines(File outpr) throws FileNotFoundException, IOException {
        Scanner sc = new java.util.Scanner(inputFile);
        List<String> wordsarraylist = new ArrayList<String>();
        while (sc.hasNextLine()) {
            wordsarraylist.add(sc.nextLine());
        }
        sc.close();
    
        Collections.reverse(wordsarraylist);
    
        FileWriter writer = new FileWriter(outpr, false);
        for (String str : wordsarraylist) {
            writer.write(str + System.lineSeparator());
        }
        writer.flush();
        writer.close();
    }
    
    public void reverseEachLine(File outpr) throws FileNotFoundException, IOException {
        Scanner sc = new Scanner(inputFile);
    
        ArrayList<List<String>> wordsarraylist = new ArrayList<List<String>>();
        while (sc.hasNextLine()) {
            String sentence = sc.nextLine();
    
            List words = Arrays.asList(sentence.split(" "));
            Collections.reverse(words);
            wordsarraylist.add(words);
        }
    
        FileWriter writer = new FileWriter(outpr, false);
        for (List<String> list : wordsarraylist) {
            for (String string : list) {
                writer.append(string + " ");
            }
            writer.append(System.lineSeparator());
        }
        writer.flush();
        writer.close();
    }
    
    public void completeReverse(File outpr) throws FileNotFoundException, IOException {
        //reverse lines first
        reverseLines(outpr);
    
        //then reverse words
        reverseEachLine(outpr);
    }
    
    私有文件输入文件;
    公共MyReverser(文件){
    this.inputFile=文件;
    }
    公共void reverseLines(文件输出)抛出FileNotFoundException、IOException{
    Scanner sc=newjava.util.Scanner(inputFile);
    List wordsarraylist=新建ArrayList();
    while(sc.hasNextLine()){
    添加(sc.nextLine());
    }
    sc.close();
    收藏。反向(单词列表);
    FileWriter writer=新的FileWriter(输出,false);
    for(字符串str:wordsarraylist){
    writer.write(str+System.lineSeparator());
    }
    西铁
    
    private File inputFile;
    
    public MyReverser(File file) {
        this.inputFile = file;
    }
    
    public void reverseLines(File outpr) throws FileNotFoundException, IOException {
        Scanner sc = new java.util.Scanner(inputFile);
        List<String> wordsarraylist = new ArrayList<String>();
        while (sc.hasNextLine()) {
            wordsarraylist.add(sc.nextLine());
        }
        sc.close();
    
        Collections.reverse(wordsarraylist);
    
        FileWriter writer = new FileWriter(outpr, false);
        for (String str : wordsarraylist) {
            writer.write(str + System.lineSeparator());
        }
        writer.flush();
        writer.close();
    }
    
    public void reverseEachLine(File outpr) throws FileNotFoundException, IOException {
        Scanner sc = new Scanner(inputFile);
    
        ArrayList<List<String>> wordsarraylist = new ArrayList<List<String>>();
        while (sc.hasNextLine()) {
            String sentence = sc.nextLine();
    
            List words = Arrays.asList(sentence.split(" "));
            Collections.reverse(words);
            wordsarraylist.add(words);
        }
    
        FileWriter writer = new FileWriter(outpr, false);
        for (List<String> list : wordsarraylist) {
            for (String string : list) {
                writer.append(string + " ");
            }
            writer.append(System.lineSeparator());
        }
        writer.flush();
        writer.close();
    }
    
    public void completeReverse(File outpr) throws FileNotFoundException, IOException {
        //reverse lines first
        reverseLines(outpr);
    
        //then reverse words
        reverseEachLine(outpr);
    }
    
    import java.util.*;
    import java.io.*;
    
    public class Reverser {
    
        Scanner sc = null;
        Scanner sc2 = null;
    
        boolean hasReadFile;
        List<String> fileLinesList;
    
        // constructor takes input file and initialize scanner sc pointing at input
        public Reverser(File file) throws FileNotFoundException, IOException {
    
            sc = new Scanner(file);
            hasReadFile = false;
            fileLinesList = new ArrayList<>();
        }
    
        // 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>();
            readFile();
            for (String sentence : fileLinesList) {
                wordsarraylist.add(sentence);
            }
    
            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 {
            readFile();
            for (String sentence : fileLinesList) {
                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();
            }
        }
    
        private void readFile() {
            if (!hasReadFile) {
                while (sc.hasNextLine()) {
                    fileLinesList.add(sc.nextLine());
                }
                fileLinesList = Collections.unmodifiableList(fileLinesList);
                hasReadFile = true;
            }
        }
    
        // 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 {
            readFile();
            for (String sentence : fileLinesList) {
                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();
    
        }
    }