【问题标题】:The output file value is null (0KB)输出文件值为空(0KB)
【发布时间】:2018-06-01 09:47:36
【问题描述】:

我正在尝试使用文件写入器写入文件,但值为空:

  FileReader inCorpus2=new FileReader("output2.txt");
       FileWriter outCorpus2=new FileWriter("Doc2(THE WANTED FILE).txt");
       Scanner sc2=new Scanner(inCorpus2);
       try{
           while(sc2.hasNextLine()){
               String tempLine=sc2.nextLine();
               Scanner sc3=new Scanner(tempLine);
       while(sc3.hasNext()){
           String temp=sc3.next();
           for(int i=0;i<UC.length;i++){
               for(int j=0;j<temp.length();j++){
                if(temp.charAt(j)==UC[i])temp=removeChar(temp,j);
               }
           }

这是错误消息:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at aya.SecondFilePreproc.main(SecondFilePreproc.java:25)

我希望我很清楚,我已尽力..请帮助我。

【问题讨论】:

  • 您可以通过使用hasNext() 而不是hasNextLine() 来解决您的问题。请参考this answer
  • 简要检查了代码,它运行良好。您应该提供一个 MCVE 来证明您的问题
  • @TuyenNguyen 相同的代码在另一个文件中工作..
  • @AdrianShum 工作正常吗? .我知道我是初学者我会尝试..
  • @yousefaqra 以防你不知道:stackoverflow.com/help/mcve

标签: java java.util.scanner filereader filewriter


【解决方案1】:

不确定是否有帮助,需要查看完整代码。只是添加了一些例外。

import java.io.*;
import java.util.Scanner;
public static String removeChar(String x,int y){
 return "something";
 }
public class SomeClass {

public static void main(String[] args) throws FileNotFoundException, IOException {
       try(FileReader inCorpus2=new FileReader("output2.txt");
           FileWriter outCorpus2=new FileWriter("Doc2(THE WANTED FILE).txt");
           Scanner sc2=new Scanner(inCorpus2)
               ){
           while(sc2.hasNextLine()){
               String tempLine=sc2.nextLine();
               Scanner sc3=new Scanner(tempLine);
       while(sc3.hasNext()){
           String temp=sc3.next();    
           //just a guess what is UC
           //char UC[]={'A','B','C'};
       }
           for(int i=0;i<UC.length;i++){
               for(int j=0;j<temp.length();j++){
                if(temp.charAt(j)==UC[i])temp=removeChar(temp,j);
               }
            }
        } 
     }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多