package org.lemon.FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*文件的字节输入流 读数据方式---一次读取一个字节
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节文件输入流对象
FileInputStream fis = new FileInputStream("abc.txt");
// 读数据
int by = 0;
while((by=fis.read())!=-1) {
//System.out.print(by);//119111109710510110105
System.out.println((char)by);
}
//释放资源
fis.close();
}
}
package org.lemon.FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*文件的字节输入流 读数据方式---一次读取一个字节数组
*/
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("abc.txt");
byte[] bys = new byte[1024];
int len = 0;
while((len=fis.read(bys))!=-1) {
System.out.println(new String(bys,0,len));
}
fis.close();
}
}
package org.lemon.CopyFile;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*需求:在当前项目下abc.txt文件的内容赋值到edf.txt文件中 采用一次一个字节----慢慢慢
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
//创建文件字节输入对象
FileInputStream fis = new FileInputStream("abc.txt");
//创建输出流对象
FileOutputStream fos = new FileOutputStream("edf.txt");
//读写数据
int by =0;
while((by=fis.read())!=-1) {
fos.write(by);
}
//释放资源
fis.close();
fos.close();
}
}
package org.lemon.CopyFile;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*E盘下有一个高圆圆.jpg文件,将它图片内容复制到当前项目下的杨桃.jpg
*/
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("e:\\高圆圆.jpg");
// 封装目的地
FileOutputStream fos = new FileOutputStream("杨桃.jpg");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fis.close();
fos.close();
}
}
package org.lemon.CopyFile;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*E盘下有一个高圆圆.jpg文件,将它图片内容复制到当前项目下的杨桃.jpg 这次快 一次一个字节数组--美滋滋
*/
public class CopyFileDemo2a {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\高圆圆.jpg");
FileOutputStream fos = new FileOutputStream("杨桃.jpg");
byte[] bys = new byte[1024];
int len = 0;
while((len= fis.read(bys))!=-1) {
fos.write(bys,0,len);
}
fis.close();
fos.close();
}
}
package org.lemon.CopyFile;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*使用字节输入流一次读取一个字节复制视频文件---->D:\\abc.mp4--->复制到当前项目:Copy.mp4
*/
public class CopyFileDemo3 {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("D:\\abc.mp4");
// 封装目的地
FileOutputStream fos = new FileOutputStream("Copy.mp4");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fis.close();
fos.close();
}
}
package org.lemon.CopyFile;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*使用字节输入流复制视频文件---->E:\\abc.mp4--->复制到当前项目:Copy.mp4
一次一个字节数组 快多了
*/
public class CopyFileDemo3a {
public static void main(String[] args) throws IOException {
// 封装数据源和目的地
FileInputStream fis = new FileInputStream("e:\\abc.mp4");
FileOutputStream fos = new FileOutputStream("Copy.mp4");
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fis.close();
fos.close();
}
}
package org.lemon.BufferedInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*字节缓冲流
* 字节缓冲输入流:BufferedInputStream
* BufferedInputStream(InputStream in) :指定默认缓冲区大小创建字节缓冲输入流对象
*/
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节缓冲流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("abc.txt"));
byte[] bys =new byte[1024];
int len = 0;
while((len=bis.read(bys))!=-1) {
System.out.println(new String(bys,0,len));
}
bis.close();
}
}
package org.lemon.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
* 字节缓冲输入流:BufferedInputStream
* 字节缓冲输出流:BufferedOutputStream
* 缓冲流只是提供一个缓冲区,针对IO进行实现数据的传输实际是通过底层基本流进行实现的,所以缓冲流不能直接对文件操作
*/
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节输出缓冲流对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("abc.txt"));
//写数据
bos.write(97);
//刷新缓冲区的流
bos.flush();
bos.close();
//flush():刷新缓冲区的流,强迫将缓冲字节都写到该流中,刷新之后是可以写数据的.
//close():关闭流对象所指向指向的这个资源文件,一旦关闭,写数据,写不进去的!
}
}
package org.lemon.Buffered;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author zhaojiangbo
*BufferedWriter:
* public void newLine():写入一个换行符号
* BufferReader:
* public String readLine():一次读取一行
*/
public class BufferedDemo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("abc.txt"));
Read(br);
write();
}
private static void Read(BufferedReader br) throws IOException {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
private static void write() throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("abc.txt"));
for(int i = 0;i<10;i++) {
bw.write("wo"+i);
bw.newLine();
bw.flush();
}
bw.close();
}
}
package org.lemon.Buffered;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedDemo2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个整数");
String str = br.readLine();
int number = Integer.parseInt(str);
System.out.println("你输入的是:"+number);
}
}
package org.lemon.Buffered;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* @author zhaojiangbo
*BufferedReader(Reader in) :创建默认缓冲大小
*/
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//方式一
int ch = 0;
while((ch=br.read())!=-1) {
System.out.println((char)ch);
}
br.close();
//方式二
char [] chs = new char[1024];
int len= 0;
while((len=br.read(chs))!=-1) {
System.out.println(new String(chs,0,len));
}
br.close();
}
}
package org.lemon.Buffered;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author zhaojiangbo
*public BufferedWriter(Writer out):创建默认缓冲区大小的一个字符缓冲输出流
*/
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("abc.txt"));
bw.write("我");
bw.write("依");
bw.write("然");
bw.write("爱");
bw.write("你");
bw.flush();
bw.close();
}
}
package org.lemon.字节流;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author zhaojiangbo
*字节流一次读取一个字节,出现了中文乱码
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("FileInputStreamDemo.java");
int by =0;
while((by=fis.read())!=-1) {
System.out.println((char)by);
}
fis.close();
}
}
package org.lemon.字节流;
import java.io.IOException;
import java.util.Arrays;
public class StringDemo {
public static void main(String[] args) throws IOException {
String s = "我爱你" ;
//编码
//不写编码格式:默认GBK
//byte[] bys = s.getBytes("GBK") ;
byte[] bys = s.getBytes() ;
System.out.println(Arrays.toString(bys));
//解码
//String str = new String(bys, "GBK") ;
String str = new String(bys) ;//默认GBK
System.out.println(str);
}
}
package org.lemon.字符流;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author zhaojiangbo
*字符输入流:字节输入流+编码格式(默认GBK)
*/
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
//创建字节输入流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("abc.txt"));
// 读数据
char[] chs = new char[1024];
int len = 0;
while((len=isr.read(chs))!=-1) {
System.out.println(new String(chs,0,len));// 转换成字符串输出
}
//释放资源
isr.close();
}
}
package org.lemon.字符流;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* @author zhaojiangbo
*字符输出流:Writer:是一个抽象类 使用的是该抽象类的子类:OutputStreamWriter
*字节输出流+编码格式---->字符输出流
*/
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"),"GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));//默认GBK
osw.write("我爱高圆圆");
osw.close();
}
}
package org.lemon.字符流;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* @author zhaojiangbo
*需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 使用字符流进行读写操作
*/
public class TestDemo {
public static void main(String[] args) throws IOException {
//创建字符转换输入流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
//封装目的地
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"));
char[] chs =new char[1024];
int len = 0;
while((len=isr.read(chs))!=-1) {
osw.write(chs,0,len);
osw.flush();//刷新流
}
isr.close();
osw.close();
}
}
package FileReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author zhaojiangbo
*需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 使用字符流进行读写操作
*使用 FileReader FileWriter 较为便捷
*/
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
char [] chs =new char [1024];
int len = 0;
while((len=fr.read(chs))!=-1) {
fw.write(chs,0,len);
fw.flush();
}
fr.close();
fw.close();
}
}