java入门10- IO基础
来源:互联网 发布:岁寒然后知松柏下一句 编辑:程序博客网 时间:2024/06/10 20:17
1、流的概念
流:数据在文件和程序(内存)之间经历的路径
输入流:数据从文件到程序(内存)的路径
输出流:数据从程序(内存)到文件的路径
按流的方向不同
输入流、输出流
按处理数据的单位不同
字节流、字符流
按功能不同
节点流、处理流
2、文件操作
java.io包中的File类
1.File 变量名 = new File(String pathname);
String rootPath="C:\\Windows\\debug";File f = new File(rootPath);
路径中用”\”表示,要有两个;
路径中用”/”表示,要有一个;
2、File 变量名 = new File(URI uri);
3、File 变量名 = new File(String parent, String child);
File f3 = new File(“d:/temp”, ”abc.txt”);
两种路径写法都能执行。
4、File 变量名 = new File(File parent, String child);
根据parent抽象路径名和child路径名字符串创建一个新File实例
File f = new File(“d:/temp”);File f4 = new File (f, ”abc.txt”);
3、File类的常用方法
4、字节流
InputStream和OutputStream分别是字节输入流和字节输出流的超类
InputStream和OutputStream是抽象类
InputStream 继承关系
-FileInputStream
-ObjectInputStream
-FilterInputStream
-BufferedInputStream
-DataInputStream
int read() //读一个字节返回
int read(byte[]) //将数据读入byte[], 返回读的字节数,返回-1表示读完。
int read(byte[], int offset, int length)
void close() //关闭此输入流并释放与该流关联的所有系统资源
FileInputStream的构造方法
1.public FileInputStream(String name) throws FileNotFoundException2.public FileInputStream(File file) throws FileNotFoundException
OutputStream 继承关系
-FileOutputStream
-FilterOutputStream
-BufferedOutputStream
-DataOutputStream
void write(int) //写一个字节
void write(byte[]) //写一个字节数组
void write(byte[], int offset, int length)
void close() //关闭此输出流并释放与此流有关的所有系统资源
void flush() //刷新此输出流并强制写出所有缓冲的输出字节
FileOutputStream的构造方法是:
- public FileOutputStream(String name) throws FileNotFoundException
- public FileOutputStream(String.name,Boolean append) throws FileNotFoundException
- public FileOutputStream(File file) throws FileNotFoundException
字节文件输入输出流
1. 创建文件输入输出流的对象
2. 打开文件
3. 用文件读写方法读写数据
4. 关闭数据流。
// 创建文件对象 File f = new File("D:\\demo\\test1.txt"); // 创建输入文件流对象 FileInputStream is = null; is = new FileInputStream(f); byte[] bytes = new byte[1024]; int n; while ((n = is.read(bytes)) != -1) { String str = new String(bytes, 0, n); System.out.println(str); }
5、字符流
Reader和Writer是字符输入输出流的抽象超类,
Reader
-BufferedReader
-InputStreamReader
-FileReader
int read(); //读单个字符
int read(char cbuf[]); //读字符放入数组中,返回-1表示读取结束
int read(char cbuf[], int offset, int length); //读字符放入数组的指定位置
void close(); //关闭此Reader并释放与其关联的所有系统资源
public FileReader(String filename)
public FileReader(File file)
Writer
-OutputStreamReader
-FileWriter
-BufferedWriter
int write(int c) ; // 写单个字符
int write(char cbuf[]) ;// 写字符数组
int write(char cbuf[], int offset, int length) ;
public FlieWriter(String filename)
public Filewriter(File file)
字符文件输入输出流
- 先创建对象打开文件
- 然后用读写方法从文件中读取数据或将数据写入文件
- 最后关闭数据流
FileReader fr = null; fr = new FileReader("D:\\demo\\test1.txt"); char[] c = new char[1024]; int n; while ((n = fr.read(c)) != -1) { String str = new String(c, 0, n); System.out.println(str); }
BufferedReader提供了readLine方法用于读取一行字符串(以\r或\n分隔)
BufferedWriter提供了newLine方法用于写入一个行分隔符
ag.
BufferedReader reader = new BufferedReader(new FileReader(inFileName));BufferedWriter writer = new BufferedWriter(new FileWriter(outFileName));
- java入门10- IO基础
- java基础入门----IO异常处理方式
- 【Java-IO】IO入门
- java IO文件操作简单基础入门例子
- java IO文件操作简单基础入门例子,IO流其实没那么难
- Java入门代码--IO
- 黑马程序员--Java基础--10IO(二)
- JAVA基础IO介绍
- java io 基础
- JAVA 基础IO流
- java IO基础学习
- java IO基础概念
- java基础之io
- java基础之IO
- java基础之IO
- java中的IO基础
- Java基础20:IO
- Java基础21:IO
- 0726 java入门
- laravel5.4 导入导出excel /.xls
- oracle与mysql数据库基本数据类型--介绍与区别
- RSA 算法原理(二)
- 并发编程--线程池ThreadPoolExecutor实现原理(二)
- java入门10- IO基础
- [bzoj4318]OSU!
- EasyDSS流媒体解决方案之视频m3u8文件总时长统计
- Less和Sass的区别
- PAT乙级 1053. 住房空置率 (20)
- Spark源码--RuleExecutor
- 行内元素与块级元素的区别,行内块级元素在IE8-的兼容性
- 安装ElasticSearch
- F