字节流与字符流
。输入流与输出流
,程序(内存)作为参照物,程序从外部读取称为输入(Input),程序向外部写数据成为输出(Output)。InputStream
不使用
byte数组读取)import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo6 {
public static void main(String[] args) throws IOException{
File file=new File("1.txt"); //新建一个文件的抽象路径
FileInputStream fis=new FileInputStream(file);
int result=fis.read();
System.out.println(result);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\1.txt");
FileInputStream fis = new FileInputStream(file);
// 单个读取
int result = fis.read();
System.out.println(result);
int res;
//循环读取
while ((res=fis.read())!=-1){
System.out.print(res+"\t");
}
}
}
byte[] bt = new byte[1024]
; //数组,增加了这个的功能就是可以逐行读取!并且效率更高!import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test2 {
/**
* 字符输入流
* @param args
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\2.txt");
FileInputStream fis = new FileInputStream(file);
byte[] bt = new byte[1024]; //数组
int count;
while ((count=fis.read(bt))!=-1){
String str = new String(bt,0,count);
System.out.println(str);
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test3 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\3.jpg");
FileInputStream fis = new FileInputStream(file);
int count;
Long start = System.currentTimeMillis();
while ((count=fis.read())!=-1){
System.out.println(count);
}
System.out.println("=========下面是所需时间============");
Long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test4 {
/**
* 字符输入流速度
* @param args
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\3.jpg");
FileInputStream fis = new FileInputStream(file);
byte[] bt = new byte[1024];
int count;
long start=System.currentTimeMillis();
while ((count=fis.read(bt))!=-1){
System.out.println(count);
}
long end=System.currentTimeMillis();
System.out.println("======下面是所用的时间======");
System.out.println(end-start);
}
}
OutputStream
是所有输出流的超类(所有父类,类同于上面的InputStrem)FileOutputStream
文件字节输出流FileOutputStream(File file) /FileOutputStream(String name)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test5 {
/**
* 字节输出流
* @param args
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\3.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write("我要和佳佳永远在一起哦!".getBytes());
System.out.println("写入成功!");
}
}
import java.io.*;
public class Test6 {
/**
* 字符输出流复制文件
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\2.txt");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File("E:\\浏览器下载资料\\IOreview\\file\\3.txt"));
byte[] bt = new byte[1024];
int count;
while ((count=fis.read(bt))!=-1){
fos.write(bt,0,count);
}
System.out.println("复制成功");
fis.close();
fos.close();
}
}
import java.io.*;
public class Test7 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\2.txt");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File("E:\\浏览器下载资料\\IOreview\\file\\3.txt"),true);
byte[] bt = new byte[1024];
int count;
while ((count=fis.read(bt))!=-1){
fos.write(bt,0,count);
}
System.out.println("复制成功");
fis.close();
fos.close();
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test9 {
/**
* 字符输入流
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\2.txt");
FileReader fr = new FileReader(file);
char[] ca = new char[1024];
//单个读取
int result = fr.read();
System.out.println("单个读取"+result);
// 循环读取
int res;
System.out.println("循环读取");
while ((res=fr.read(ca))!=-1){
String str = new String(ca,0,res);
System.out.println(str);
System.out.println(res+"\t");
}
}
}
Writer
Filewriter
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test10 {
/**
* 字符输出流
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\2.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(new File("E:\\浏览器下载资料\\IOreview\\file\\4.txt"));
char[] chars = new char[1024];
int count;
while ((count=fr.read(chars))!=-1){
fw.write(chars,0,count);
}
System.out.println("字符输出流复制成功");
fr.close();
fw.close();
}
}
BufferedReader/BufferedWriter
带缓冲区的字符输入流与字符输出流。BufferedReader br = new BufferedReader(fr);
,前面的两句是一模一样的!import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test11 {
/**
* 字符缓冲输入流
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\2.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
while ((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
}
}
import java.io.*;
public class Test12 {
/**
* 字符缓冲输出流
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\浏览器下载资料\\IOreview\\file\\2.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(new File("E:\\浏览器下载资料\\IOreview\\file\\5.txt"));
BufferedWriter bw = new BufferedWriter(fw);
char[] chars = new char[1024];
int count;
while ((count=br.read(chars))!=-1){
bw.write(chars,0,count);
}
System.out.println("字符·缓冲输出流复制成功");
bw.close();
br.close();
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/122470003
内容来源于网络,如有侵权,请联系作者删除!
InputStream
不使用
byte数组读取)byte[] bt = new byte[1024]
; //数组,增加了这个的功能就是可以逐行读取!并且效率更高!Writer
BufferedReader/BufferedWriter
带缓冲区的字符输入流与字符输出流。BufferedReader br = new BufferedReader(fr);
,前面的两句是一模一样的!