package p02.c02.WenJianHeLiu;
import java.io.*;
public class FileInputStreamDemo {
/*
* FileInputStream类的用法
* FileInputStream类称为文件输入流,继承于InputStream类,是进行文件读操作的最基本类
* 作用是将文件中的数据输入到内存中,我们可以利用它来读文件。
* 由于它属于字节流,因此在读取'Unicode'字符(如中文)的文件时可能会出现问题。
*/
/*
* 一、创建文件输入流对象:
* FileInputStream类的构造方法有3种重载方式,以下是常用的几种
* 方法1
* FileInputStream f =new FileInputStream(Fail fail);
* //使用File对象创建文件输入流对象。
* 方法2
* FileInputStream f =new FileInputStream(String name);
* //使用文件名或者路径创建文件输入流对象。
*
* 二、读取文件中的数据
* 方法1
* fis.read(); 读取文件中的数据,一次读取一个字节,读取的数据作为返回值返回,
* 如果读到文件末尾,则返回-1,有可能抛异常,必须捕捉。
* 方法2
* fis.read(byte[] b);
* 读取文件中的数据,将读到的数据存放到byte型数组中,并返回读取的字节的数量。
* 未读到数据返回-1,有可能抛异常,必须捕捉。
*
* 三、关闭流对象
* fis.close 关闭流对象,有可能抛异常,必须捕捉
*/
public static void main(String[] args) {
File f = new File("e:\\java\\a.txt");
try {
//创建FileInputStream的对象fis,以方便调用FileInputStream类的方法
FileInputStream fis = new FileInputStream(f);
try {
for (int i = 0; i < f.length(); i++) {
/*
* 强制类型转换
* 具体规则为:double→float→long→int→short(char)→byte
* 语法格式为:(转换到的类型)需要转换的值
*/
char ch = (char)fis.read();
System.out.print(ch);
}
try {
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main3(String[] args) {
File f = new File("E:\\java\\a.txt");
try {
FileInputStream fis = new FileInputStream(f);
System.out.println("文件读取成功");
/*
* 方法2、fis.read(byte[] b);
* 读取文件中的数据,将读到的数据存放到byte型数组中,并返回读取的字节的数量。
* 未读到数据返回-1,有可能抛异常,必须捕捉。
*/
byte[] b = new byte[(int)f.length()];
try {
//读取文件中的数据,将读到的数据存放到byte型数组中
fis.read(b);
} catch (IOException e) {
e.printStackTrace();
}
/*
* Java中String与byte[]的转换
* String s = "fs123fdsa"; // String变量
* byte[] b = s.getBytes(); // String转换为byte[] b
* String t = new String(b); // byte[] b转换为String
*/
String str = new String(b);
System.out.println(str);
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class FileInputStreamDemo {
/*
* FileInputStream类的用法
* FileInputStream类称为文件输入流,继承于InputStream类,是进行文件读操作的最基本类
* 作用是将文件中的数据输入到内存中,我们可以利用它来读文件。
* 由于它属于字节流,因此在读取'Unicode'字符(如中文)的文件时可能会出现问题。
*/
/*
* 一、创建文件输入流对象:
* FileInputStream类的构造方法有3种重载方式,以下是常用的几种
* 方法1
* FileInputStream f =new FileInputStream(Fail fail);
* //使用File对象创建文件输入流对象。
* 方法2
* FileInputStream f =new FileInputStream(String name);
* //使用文件名或者路径创建文件输入流对象。
*
* 二、读取文件中的数据
* 方法1
* fis.read(); 读取文件中的数据,一次读取一个字节,读取的数据作为返回值返回,
* 如果读到文件末尾,则返回-1,有可能抛异常,必须捕捉。
* 方法2
* fis.read(byte[] b);
* 读取文件中的数据,将读到的数据存放到byte型数组中,并返回读取的字节的数量。
* 未读到数据返回-1,有可能抛异常,必须捕捉。
*
* 三、关闭流对象
* fis.close 关闭流对象,有可能抛异常,必须捕捉
*/
public static void main(String[] args) {
File f = new File("e:\\java\\a.txt");
try {
//创建FileInputStream的对象fis,以方便调用FileInputStream类的方法
FileInputStream fis = new FileInputStream(f);
try {
for (int i = 0; i < f.length(); i++) {
/*
* 强制类型转换
* 具体规则为:double→float→long→int→short(char)→byte
* 语法格式为:(转换到的类型)需要转换的值
*/
char ch = (char)fis.read();
System.out.print(ch);
}
try {
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main3(String[] args) {
File f = new File("E:\\java\\a.txt");
try {
FileInputStream fis = new FileInputStream(f);
System.out.println("文件读取成功");
/*
* 方法2、fis.read(byte[] b);
* 读取文件中的数据,将读到的数据存放到byte型数组中,并返回读取的字节的数量。
* 未读到数据返回-1,有可能抛异常,必须捕捉。
*/
byte[] b = new byte[(int)f.length()];
try {
//读取文件中的数据,将读到的数据存放到byte型数组中
fis.read(b);
} catch (IOException e) {
e.printStackTrace();
}
/*
* Java中String与byte[]的转换
* String s = "fs123fdsa"; // String变量
* byte[] b = s.getBytes(); // String转换为byte[] b
* String t = new String(b); // byte[] b转换为String
*/
String str = new String(b);
System.out.println(str);
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}