Java 输出文件值为空(0KB)
我正在尝试使用file writer写入文件,但该值为空:Java 输出文件值为空(0KB),java,java.util.scanner,filereader,filewriter,Java,Java.util.scanner,Filereader,Filewriter,我正在尝试使用file writer写入文件,但该值为空: FileReader inCorpus2=new FileReader("output2.txt"); FileWriter outCorpus2=new FileWriter("Doc2(THE WANTED FILE).txt"); Scanner sc2=new Scanner(inCorpus2); try{ while(sc2.hasNextLine()){
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);
}
}
我希望我是清楚的,我已经尽力了。。请帮帮我。不确定是否有帮助,需要查看完整代码。只是增加了一些例外。
import java.io.*;
导入java.util.Scanner;
公共静态字符串removeChar(字符串x,int y){
返回“某物”;
}
公共类{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
try(FileReader inCorpus2=newfilereader(“output2.txt”);
FileWriter outCorpus2=新的FileWriter(“Doc2(通缉文件).txt”);
扫描仪sc2=新扫描仪(inCorpus2)
){
while(sc2.hasNextLine()){
字符串tempLine=sc2.nextLine();
扫描仪sc3=新扫描仪(模板线);
while(sc3.hasNext()){
字符串temp=sc3.next();
//猜猜什么是UC
//字符UC[]={'A','B','C'};
}
对于(int i=0;i您可以使用hasNext()
而不是hasNextLine()来解决您的问题
。请参阅。对代码进行了简要检查,运行良好。您应该提供MCVE来演示您的problem@TuyenNguyen相同的代码在另一个文件中工作。@AdrianShum工作正常吗?。我知道我是初学者,我会试试。@yousefaqra,如果你不知道的话:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at aya.SecondFilePreproc.main(SecondFilePreproc.java:25)
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);
}
}
}
}
}
}