博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字节流(笔记)
阅读量:4598 次
发布时间:2019-06-09

本文共 4503 字,大约阅读时间需要 15 分钟。

字节流

File 的构造方法

File(File parent,String child)  抽象路径名+child路径名字创建File对象

File(String pathname) 路径名字符串转化为抽象路径名创建一个新File实例

File(String p, String c) 路径字符串和路径字符串创建File对象

 

File 的方法

getAbsolutePath()     获取绝对路径

getName()    获取文件名

getPath()   获取路径名字

length()  返回抽象路径名表示的文件长度

 

 

字节流

OutputStream抽象类       子类FileInputStream  主要方法 read()

 

 InputStream 抽象类   子类FileOutputStream  主要方法write()

public class FileOutputStreamDemo {	public static void main(String[] args) throws IOException {		//需求:将数据写入到文件中。		//创建存储数据的文件。		File file = new File("c:\\file.txt");		//创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。		//输出流目的是文件,会自动创建。如果文件存在,则覆盖。		FileOutputStream fos = new FileOutputStream(file);		//调用父类中的write方法。		byte[] data = "abcde".getBytes();		fos.write(data);		//关闭流资源。		fos.close();	}}

 

1 public class FileOutputStreamDemo2 {2     public static void main(String[] args) throws Exception {3         File file = new File("c:\\file.txt");4         FileOutputStream fos = new FileOutputStream(file, true);[设置给指定文件续写数据]5         String str = "\r\n"[实现换行]+"itcast";6         fos.write(str.getBytes());7         fos.close();8     }9 }
FileOutputStream(file, true)不覆盖原文件,而是往文件末尾继续添加

 

public class FileInputStreamDemo {    public static void main(String[] args) throws IOException {        File file = new File("c:\\file.txt");        //创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。        FileInputStream fis = new FileInputStream(file);        //读取数据。使用 read();一次读一个字节。        int ch = 0;        while((ch=fis.read())!=-1){            System.out.pr        }intln("ch="+(char)ch);        // 关闭资源。        fis.close();    }}

创建一个字节数组,一次性读入一个数组大小的内容

public class FileInputStreamDemo2 {	public static void main(String[] args) throws IOException {		/*		 * 演示第二个读取方法, read(byte[]);		 */		File file = new File("c:\\file.txt");		// 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。		FileInputStream fis = new FileInputStream(file);				//创建一个字节数组。		byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。				int len = 0;		while((len=fis.read(buf))!=-1){			System.out.println(new String(buf,0,len));		}		fis.close();	}}

 字节流的复制代码实现

public class CopyFileTest {    public static void main(String[] args) throws IOException {        //1,明确源和目的。        File srcFile = new File("c:\\YesDir\test.JPG");        File destFile = new File("copyTest.JPG");                //2,明确字节流 输入流和源相关联,输出流和目的关联。        FileInputStream fis = new FileInputStream(srcFile);        FileOutputStream fos = new FileOutputStream(destFile);                //3, 使用输入流的读取方法读取字节,并将字节写入到目的中。        int ch = 0;        while((ch=fis.read())!=-1){            fos.write(ch);        }        //4,关闭资源。        fos.close();        fis.close();    }}

字节流数组方式实现复制文件

public class CopyFileByBufferTest {    public static void main(String[] args) throws IOException {        File srcFile = new File("c:\\YesDir\test.JPG");        File destFile = new File("copyTest.JPG");        // 明确字节流 输入流和源相关联,输出流和目的关联。        FileInputStream fis = new FileInputStream(srcFile);        FileOutputStream fos = new FileOutputStream(destFile);        //定义一个缓冲区。        byte[] buf = new byte[1024];        int len = 0;        while ((len = fis.read(buf)) != -1) {            fos.write(buf, 0, len);// 将数组中的指定长度的数据写入到输出流中。        }        // 关闭资源。        fos.close();        fis.close();    }}

字节流读取字符文件

package Io;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IODemo {    public static void main(String[] args) throws IOException {//        writeDemo();        readDemo();            }    private static void readDemo() throws IOException {        File file = new File("F://a//one.txt");        try {            FileInputStream fis = new FileInputStream(file);            int len=0;            byte [] b =new byte [1024];            while((len=fis.read(b))!=-1) {                System.out.println(new String(b));            }                                } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }    private static void writeDemo() throws IOException {        File file = new File("F://a//one.txt");        try {            FileOutputStream fos =    new FileOutputStream(file);                        fos.write("hellow,word".getBytes());                    } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }             }}

 

转载于:https://www.cnblogs.com/lyjblogs/p/7886024.html

你可能感兴趣的文章
bzoj 3172 AC自动机
查看>>
rabbitmq
查看>>
解决Latex中Itemize距离过大的问题
查看>>
1打印沙漏
查看>>
LeetCode | Rotate List
查看>>
CodeForces - 455D
查看>>
【转】Django模糊查询
查看>>
Bugtags 创业一年总结
查看>>
UML建模原理
查看>>
[BZOJ 1083] [SCOI2005] 繁忙的都市
查看>>
图解C#的值类型,引用类型,栈,堆,ref,out
查看>>
spring5.0版本-AOP-如何实现拦截器链式调用(责任链模式)
查看>>
dht11 temperature & humidity sensor v2
查看>>
selenium 启动 IE11
查看>>
习题6.6
查看>>
系统分析与设计第三次作业
查看>>
Redis——非阻塞IO和队列
查看>>
iPad最值得期待的切实改进构想
查看>>
(转载)ERROR :“dereferencing pointer to incomplete type”是什么错误?
查看>>
jstack 堆栈日志分析
查看>>