IO-03字节流字符流
来源:互联网 发布:如何禁止卸载软件 编辑:程序博客网 时间:2024/04/29 02:21
在程序中所有的数据都是以流的方式进行传输和保存的,程序需要数据的时候要使用输入流读取数据,而程序需要将一些数据保存起来则需要使用输出流来完成。
在JAVA.IO包中操作文件内容的主要有两大类:字节流,字符流,两类都分为输入和输出操作。在字节流中输出数据主要是使用OutputStream,输入主要使用InputStream,在字符流中输出主要使用Writer类完成,输入只要使用Reader类完成。
主要的操作流程如下:
- 使用FILE类打开一个文件
- 通过字节流或者字符流的子类,指定输出的位置。
- 进行读/写操作
- 关闭输入/输出
字节流输出操作代码:
- package lianxijihe;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- public class lianxi043 {
- public static void main(String[] args){
- //1.使用File类找到一个文件
- File f = new File("F:\\aaa.txt");
- //2.通过子类实例化父类
- try {
- OutputStream out = new FileOutputStream(f);
- //3.进行读写操作
- String ss = "hello";
- byte[] b = ss.getBytes();
- out.write(b);
- //关闭输出流
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- package lianxijihe;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- public class lianxi043 {
- public static void main(String[] args){
- //1.使用File类找到一个文件
- File f = new File("F:\\aaa.txt");
- //2.通过子类实例化父类
- try {
- OutputStream out = new FileOutputStream(f);
- //3.进行读写操作
- String ss = "hello1111";
- byte[] b = ss.getBytes();
- for(int i=0;i<b.length;i++){//采用循环写入,每次只写入1位
- out.write(b[i]);
- }
- //关闭输出流
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
如果不想覆盖之前文件,需要执行追加操作:
- package lianxijihe;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- public class lianxi043 {
- public static void main(String[] args){
- //1.使用File类找到一个文件
- File f = new File("F:\\aaa.txt");
- //2.通过子类实例化父类
- try {
- OutputStream out = new FileOutputStream(f, true);
- //3.进行读写操作
- String ss = "hello1111";
- byte[] b = ss.getBytes();
- for(int i=0;i<b.length;i++){//采用循环写入,每次只写入1位
- out.write(b[i]);
- }
- //关闭输出流
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- package lianxijihe;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- public class lianxi043 {
- public static void main(String[] args){
- //1.使用File类找到一个文件
- File f = new File("F:\\aaa.txt");
- //2.通过子类实例化父类
- try {
- OutputStream out = new FileOutputStream(f, true);
- //3.进行读写操作
- String ss = "\r\nhello1111";
- byte[] b = ss.getBytes();
- for(int i=0;i<b.length;i++){//采用循环写入,每次只写入1位
- out.write(b[i]);
- }
- //关闭输出流
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
字节流输入操作代码:
- package lianxijihe;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- public class lianxi044 {
- public static void main(String[] args){
- File f = new File("f:\\abc.txt");
- try {
- InputStream in = new FileInputStream(f);
- byte[] b = new byte[(int)f.length()];
- in.read(b);
- System.out.println(new String(b));
- in.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
一位一位读进来
- package lianxijihe;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- public class lianxi044 {
- public static void main(String[] args){
- File f = new File("f:\\abc.txt");
- try {
- InputStream in = new FileInputStream(f);
- byte[] b = new byte[(int)f.length()];
- for(int i=0;i<b.length;i++){
- b[i] =(byte)in.read();
- }
- System.out.println(new String(b));
- in.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
如果不知道文件的大小则:
- package lianxijihe;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- public class lianxi044 {
- public static void main(String[] args){
- File f = new File("f:\\abc.txt");
- try {
- InputStream in = new FileInputStream(f);
- byte[] b = new byte[1024];
- int len =0;
- int tem=0;
- while((tem = in.read())!=-1){
- b[len] = (byte)tem;
- len++;
- }
- System.out.println(new String(b,0,len));
- in.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
字符流输出:
- package lianxijihe;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- public class lianxi045 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- File f = new File("f:\\abc.txt");
- try {
- Writer w = new FileWriter(f);
- String ss = "aaaaaaaaaaaaa";
- w.write(ss);
- w.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
字符流读取数据:
- package lianxijihe;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- public class lianxi045 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- File f = new File("f:\\abc.txt");
- try {
- Reader r = new FileReader(f);
- char[] c = new char[1024];
- r.read(c);
- System.out.println(new String(c));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
字节流与字符流的区别:
字节流在操作的时候本身不会用到缓冲区,是与本件本身直接操作的,而字符流在操作的时候是使用到缓冲区的。
在所有的硬盘上保存的文件或者是进行传输的时候是以字节的方式进行的。包括图片也是按字节完成的,而字符只有在内存中才会形成,使用字节的操作是最多的。
复制文件的小程序:
- package lianxijihe;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class lianxi046 {
- public static void main(String[] args){
- if(args.length!=2){
- System.out.println("输入的格式不对");
- System.exit(1);
- }
- File f1 = new File(args[0]);
- File f2 = new File(args[1]);
- if(!f1.exists()){
- System.out.println("源文件不存在");
- System.exit(1);
- }
- try {
- InputStream in = new FileInputStream(f1);
- OutputStream out = new FileOutputStream(f2);
- int temp =0;
- while((temp=in.read())!=-1){
- out.write(temp);
- }
- in.close();
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
0 0
- IO-03字节流字符流
- IO:字符流、字节流
- IO流---字节字符流
- IO(字节流、字符流)
- IO-字节、字符转换流
- File io 字节字符流
- IO流(概述字节流字符流)
- IO流字节流字符流学习
- 7.IO流 字节流/字符流
- IO流输入输出流,字符字节流
- IO分两种流:字节流和字符流
- Java IO字符流和字节流
- Java IO:字节流与字符流
- io字节流和字符流
- Java IO字符流与字节流
- 字符流与字节流(IO)
- IO-字节流与字符流
- Java-IO:字节流和字符流
- Mysql编写定时任务事件
- as3学习笔记6:创建自定义按钮
- 缓存介绍
- Top 10 Mistakes that Python Programmers Make
- 结构体的自身引用
- IO-03字节流字符流
- 古代赌局 俗话说:十赌九输。(转载)
- eclipse启动时 failed to create the java virtual machine 解决办法
- c++队列链表模板类
- 《漫画线性代数》读书笔记 矩阵
- IOS 函数内 autorelease release
- 年轻工程师是如何锻炼成“高手”的
- jquery slider游标step
- IOS7.1 企业应用 证书无效 已解决