没有合适的资源?快使用搜索试试~ 我知道了~
多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内容
资源推荐
资源详情
资源评论
\
一、多种方式读文件内容。
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
Java代码
1.import java.io.BufferedReader;
2.import java.io.File;
3.import java.io.FileInputStream;
4.import java.io.FileReader;
5.import java.io.IOException;
6.import java.io.InputStream;
7.import java.io.InputStreamReader;
8.import java.io.RandomAccessFile;
9.import java.io.Reader;
10.
11.public class ReadFromFile {
12. /**
13. * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
14. *
15. * @param fileName
16. * 文件的名
17. */
18. public static void readFileByBytes(String fileName) {
19. File file = new File(fileName);
20. InputStream in = null;
21. try {
一、多种方式读文件内容。
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
Java代码
1.import java.io.BufferedReader;
2.import java.io.File;
3.import java.io.FileInputStream;
4.import java.io.FileReader;
5.import java.io.IOException;
6.import java.io.InputStream;
7.import java.io.InputStreamReader;
8.import java.io.RandomAccessFile;
9.import java.io.Reader;
10.
11.public class ReadFromFile {
12. /**
13. * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
14. *
15. * @param fileName
16. * 文件的名
17. */
18. public static void readFileByBytes(String fileName) {
19. File file = new File(fileName);
20. InputStream in = null;
21. try {
22. System.out.println("以字节为单位读取文件内容,一次读一个字节:");
23. // 一次读一个字节
24. in = new FileInputStream(file);
25. int tempbyte;
26. while ((tempbyte = in.read()) != -1) {
27. System.out.write(tempbyte);
28. }
29. in.close();
30. } catch (IOException e) {
31. e.printStackTrace();
32. return;
33. }
34. try {
35. System.out.println("以字节为单位读取文件内容,一次读多个字节:");
36. // 一次读多个字节
37. byte[] tempbytes = new byte[100];
38. int byteread = 0;
39. in = new FileInputStream(fileName);
40. ReadFromFile.showAvailableBytes(in);
41. // 读入多个字节到字节数组中,byteread为一次读入的字节数
42. while ((byteread = in.read(tempbytes)) != -1) {
43. System.out.write(tempbytes, 0, byteread);
44. }
45. } catch (Exception e1) {
46. e1.printStackTrace();
47. } finally {
48. if (in != null) {
49. try {
50. in.close();
51. } catch (IOException e1) {
23. // 一次读一个字节
24. in = new FileInputStream(file);
25. int tempbyte;
26. while ((tempbyte = in.read()) != -1) {
27. System.out.write(tempbyte);
28. }
29. in.close();
30. } catch (IOException e) {
31. e.printStackTrace();
32. return;
33. }
34. try {
35. System.out.println("以字节为单位读取文件内容,一次读多个字节:");
36. // 一次读多个字节
37. byte[] tempbytes = new byte[100];
38. int byteread = 0;
39. in = new FileInputStream(fileName);
40. ReadFromFile.showAvailableBytes(in);
41. // 读入多个字节到字节数组中,byteread为一次读入的字节数
42. while ((byteread = in.read(tempbytes)) != -1) {
43. System.out.write(tempbytes, 0, byteread);
44. }
45. } catch (Exception e1) {
46. e1.printStackTrace();
47. } finally {
48. if (in != null) {
49. try {
50. in.close();
51. } catch (IOException e1) {
剩余24页未读,继续阅读
资源评论
maowei0802
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功