文件的简单读取,FileInputStream类的简单使用
来源:互联网 发布:淘宝加购物车软件 编辑:程序博客网 时间:2024/05/21 18:43
文件输入操作:
FileInputStream类的使用:
1、定义一个数组、整形数、字符串、存放文本的路径;
2、创建一个FileInputStream类对象,read()方法将读取数据放入数组并返回内容的长度;
3、整形数接收read()返回值,获取长度;
0 - 数组索引的起始位置;
示例:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
public class FileInputOpration {
public static void main(String[] args) {
String Url = "C:/Users/admin/Desktop/myjava/AreaGirth.java";
byte[] bt = new byte[2048];
int bytes;
try {
FileInputStream fileIn = new FileInputStream(Url);
bytes = fileIn.read(bt);
String str = new String(bt, 0, bytes);//后文有文档说明该方法
System.out.println(str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
附加String构造方法文档提示:
String
public String(byte[] bytes,
int offset,
int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset. The length of the
new String is a function of the charset, and hence may not be equal to the length of the subarray.
The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The
CharsetDecoder class should be used when more control over the decoding process is required.
Parameters:
bytes - The bytes to be decoded into characters
offset - The index of the first byte to decode
length - The number of bytes to decode
Throws:
IndexOutOfBoundsException - If the offset and the length arguments index characters outside the bounds of the bytes array
}
FileInputStream类的使用:
1、定义一个数组、整形数、字符串、存放文本的路径;
2、创建一个FileInputStream类对象,read()方法将读取数据放入数组并返回内容的长度;
3、整形数接收read()返回值,获取长度;
4、字符串对象利用构造方法接收数据。
String str=new String(bt,0,bytes);//内容,起始位置下标,长度
0 - 数组索引的起始位置;
bytes - 转换的最大长度;
示例:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
public class FileInputOpration {
public static void main(String[] args) {
String Url = "C:/Users/admin/Desktop/myjava/AreaGirth.java";
byte[] bt = new byte[2048];
int bytes;
try {
FileInputStream fileIn = new FileInputStream(Url);
bytes = fileIn.read(bt);
String str = new String(bt, 0, bytes);//后文有文档说明该方法
System.out.println(str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
附加String构造方法文档提示:
String
public String(byte[] bytes,
int offset,
int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset. The length of the
new String is a function of the charset, and hence may not be equal to the length of the subarray.
The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The
CharsetDecoder class should be used when more control over the decoding process is required.
Parameters:
bytes - The bytes to be decoded into characters
offset - The index of the first byte to decode
length - The number of bytes to decode
Throws:
IndexOutOfBoundsException - If the offset and the length arguments index characters outside the bounds of the bytes array
}
0 0
- 文件的简单读取,FileInputStream类的简单使用
- IO:使用FileInputStream的简单示例
- IO:使用FileInputStream的简单示例2
- Java -- FileInputStream与FileOutputStream的简单使用
- java的FileInputStream类读取文件
- 使用FileInputStream的简单示例:及复杂示例(创建文件夹和文件,像文件写入东西并从文件读取东西)
- 简单的文件读取
- dom4j读取xml文件的简单使用
- 51.如何使用FileInputStream读取其他文件的内容
- 文件输入输出流的简单原理---fileoutputstream/fileinputstream
- FileInputStream和FileOutputStream实现简单的文件复制
- 文件读入演示FileInputStream类的使用
- 使用FileReader类、BufferedReader类、FileInputStream类三种方式进行文件的读取
- IO的FileInputStream读取文件流程
- FileInputStream读取磁盘内的文件
- FileInPutStream类的使用
- 简单的读取文件内容
- 简单的文件读取实例
- 《算法导论》实验五:最近点对算法(C++)
- Web.config配置文件详解(新手必看)
- maven项目的library被删,我这么做的
- 界面跳转之presentViewController、pushViewController
- 基于nginx+HLS的流媒体服务器搭建(Ubuntu 14.04系统)
- 文件的简单读取,FileInputStream类的简单使用
- $.inArray函数用法
- Ubuntu12.04+QtCreator2.3.0+Qt4.8.1+opencv2.4.9(一)
- STM32F303X单片机USB例程详细解析3
- 对文件封装util
- 打印Http相应头字段的方法
- activity 生命周期演示
- linux每日学习(二)额外补充
- 蓝桥杯基础练习 十进制转换16进制