package com . yqq . app6 ; import java . io . FileInputStream ; import java . io . FileNotFoundException ; import java . io . IOException ; /** * @Author yqq * @Date 2021/11/11 19:58 * @Version 1.0 */ public class FirstDemo { public static
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @Author yqq
* @Date 2021/11/11 19:58
* @Version 1.0
*/
public class FirstDemo {
public static void main(String[] args) {
FileInputStream fis = null;
//创建字节输入流对象
try {
fis = new FileInputStream("D:/var/wc.txt");
//read() 一次只能读取一个字符的ascii码
int s1 = fis.read();//打印输入第一个字符对应的ASCII码
int s2 = fis.read();//打印输入第二个字符对应的ASCII码
System.out.println(s1+"-"+s2);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
那么如何读取完整个文件和输出原本字符呢?
import java.io.FileInputStream;
import java.io.IOException;
/**
* @Author yqq
* @Date 2021/11/11 20:09
* @Version 1.0
*/
public class SecondDemo {
public static void main(String[] args) {
FileInputStream fis = null;
try {
//创建字节输入流对象
fis = new FileInputStream("D:/var/wc.txt");
StringBuilder sb = new StringBuilder();
int temp = 0;
while((temp = fis.read())!=-1){
System.out.println(temp);
sb.append((char) temp);
}
System.out.println(sb.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}