输出地文件内容不知为何为assic码 这是我的程序
import java.io.*;
public class PrgrmStream{
public static void main (String[] args) {
try{
FileInputStream file =new FileInputStream("TestText.txt"); boolean eof = false;
int bytecount=0;
int stringcount=0;
int linecount=0;
while(!eof){
int input = file.read();
System.out.print(input);
if(input==-1)
eof=true;
else if(input==' ')
stringcount++;
else if(input=='\n')
linecount++;
else
bytecount++;
}
file.close(); //关闭文件
System.out.println("\nBytes read:"+(bytecount+1)+"\nStrings read:"+(stringcount+1)+"\nLines read:"+(linecount+1));
}
catch(IOException e){
System.out.println("Error--"+e.toString());
}
}
}
import java.io.*;
public class PrgrmStream{
public static void main (String[] args) {
try{
FileInputStream file =new FileInputStream("TestText.txt"); boolean eof = false;
int bytecount=0;
int stringcount=0;
int linecount=0;
while(!eof){
int input = file.read();
System.out.print(input);
if(input==-1)
eof=true;
else if(input==' ')
stringcount++;
else if(input=='\n')
linecount++;
else
bytecount++;
}
file.close(); //关闭文件
System.out.println("\nBytes read:"+(bytecount+1)+"\nStrings read:"+(stringcount+1)+"\nLines read:"+(linecount+1));
}
catch(IOException e){
System.out.println("Error--"+e.toString());
}
}
}
BufferedReader用你的fileInputStream转换成inputStreamReader初始化
System.out.print((char) input);//input为int型变量,只有将其强制转换成字符型才行
private int count = 0;
/**
*
* @param filePath
* @return read the file to record line;
*/
private FileOpenTest(String filePath) {
File f = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(f));
while (br.ready()) {
br.readLine();
count ++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private int getCount() {
return count;
}
public static void main(String[] args) {
String filePath = "D:\\MrTool.java"; //local file root
FileOpenTest fot = new FileOpenTest(filePath);
System.out.println(fot.getCount());
}
}