JAVA程序目标实现文件夹路径下所有文件批量的行数、元素数统计,提示如图错误,请问程序错在哪儿了?
importjava.io.*;importjava.util.*;importjava.lang.String;publicclassXml2{publicstatic...
import java.io.*;
import java.util.*;
import java.lang.String;
public class Xml2 {
public static void main(String[] args) throws Exception{
Scanner input=new Scanner(System.in);
System.out.println("请输入路径");
String path=input.next();
System.out.println(path);
File fileDir=new File(path);
String[] files =fileDir.list();
for (String file:files)
{
System.out.println(file);
int charNum= 0 ; //字符数
int wordsNum= 0; //单词数
int lineNum = 0; //行数
int eleNum= 0 ; //元素数
int endControl=0 ;//控制何时结束
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(isr);
String s;
while(endControl<10)
{
while( (s = br.readLine())!= null && !s.equals("")){
s.replace(' ',' '); //将tab转换为空格,方便控制
String p = s.trim();
charNum+=p.length();
wordsNum+=p.split(" ").length;
lineNum ++;
// while(s.charAt(0) == ' ') s = s.substring(1, s.length());
System.out.println( p.substring(0) );
if(p.startsWith("<") && !p.startsWith("</"))
{
eleNum++;
}
endControl=0;
}
endControl++;
}
isr.close();//关闭
System.out.println("字符数:"+charNum+"\t单词数:"+wordsNum+"\t行数:"+lineNum+"\t元素数:"+eleNum);
}
}
} 展开
import java.util.*;
import java.lang.String;
public class Xml2 {
public static void main(String[] args) throws Exception{
Scanner input=new Scanner(System.in);
System.out.println("请输入路径");
String path=input.next();
System.out.println(path);
File fileDir=new File(path);
String[] files =fileDir.list();
for (String file:files)
{
System.out.println(file);
int charNum= 0 ; //字符数
int wordsNum= 0; //单词数
int lineNum = 0; //行数
int eleNum= 0 ; //元素数
int endControl=0 ;//控制何时结束
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(isr);
String s;
while(endControl<10)
{
while( (s = br.readLine())!= null && !s.equals("")){
s.replace(' ',' '); //将tab转换为空格,方便控制
String p = s.trim();
charNum+=p.length();
wordsNum+=p.split(" ").length;
lineNum ++;
// while(s.charAt(0) == ' ') s = s.substring(1, s.length());
System.out.println( p.substring(0) );
if(p.startsWith("<") && !p.startsWith("</"))
{
eleNum++;
}
endControl=0;
}
endControl++;
}
isr.close();//关闭
System.out.println("字符数:"+charNum+"\t单词数:"+wordsNum+"\t行数:"+lineNum+"\t元素数:"+eleNum);
}
}
} 展开
1个回答
展开全部
错误在你的控制台输出new2.txt这一项就可以看出来
大概25行处
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
这里传进来的值是new2.txt.正确的值应该是c:/test/new2.txt
所以应该改成InputStreamReader isr = new InputStreamReader(new FileInputStream(path+"/"+file));
或者InputStreamReader isr = new InputStreamReader(new FileInputStream(path+file));
要看你输入文件夹的时候怎么输入了
大概25行处
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
这里传进来的值是new2.txt.正确的值应该是c:/test/new2.txt
所以应该改成InputStreamReader isr = new InputStreamReader(new FileInputStream(path+"/"+file));
或者InputStreamReader isr = new InputStreamReader(new FileInputStream(path+file));
要看你输入文件夹的时候怎么输入了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询