目录
一、测试FileInputStream和FileOutStream的使用
一、测试FileInputStream和FileOutStream的使用
public class FileInputOutputStreamTest {
@Test
public void test(){
File file = new File("hello.txt");
FileInputStream fileInputStream=null;
try {
fileInputStream=new FileInputStream(file);
byte[] bytes=new byte[5];
int len;
while ((len=fileInputStream.read(bytes))!=-1){
String st=new String(bytes,0,len);
//println加上ln每读完len长度的文件自动换行
System.out.println(st);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
}
}
}
}
/*
复制图片
*/
@Test
public void test1(){
File file = new File("干饭.jpg");
File file1 = new File("干饭人.jpg");
FileInputStream fileInputStream =null;
FileOutputStream fileOutputStream =null;
try {
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file1);
byte[] chars=new byte[5];
int num;
while ((num = fileInputStream.read(chars)) != -1){
// String str=new String(byte,0,num);
//赋值操作
fileOutputStream.write(chars,0,num);
}
System.out.println("赋值完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//造一个快捷赋值方法,只需要通过改变参数值就行
public void sanqu(String fist,String serack){
File file = new File(fist);
File file1 = new File(serack);
FileInputStream fileInputStream =null;
FileOutputStream fileOutputStream =null;
try {
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file1);
//数组长度大小影响传输速度
// byte[] chars=new byte[5];//3127
byte[] chars=new byte[1024];//23
int num;
while ((num = fileInputStream.read(chars)) != -1){
// String str=new String(byte,0,num);
//赋值操作
fileOutputStream.write(chars,0,num);
}
System.out.println("赋值完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test2(){
long start=System.currentTimeMillis();
String fist="D:\\WenJian\\QQ\\添朵\\Image.png";
String send="花花4.png";
sanqu(fist,send);
long end=System.currentTimeMillis();
System.out.println("操作时间为:"+(end-start));
}
}
二、测试FileReader和FileWriter的使用
public class FileReaderWriterTest1 {
@Test
public void test(){
File file = new File("hello.txt");//相较于工程下
System.out.println(file.getAbsolutePath());
File file1 = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\hello.txt");
System.out.println(file1.getAbsolutePath());
}
/*
将D:\WenJian\IDEA\Java高级基础部分\Day08输入输出流\hello.txt下的hello.txt文件内容读入内存,
并输入控制台
*/
@Test
public void test1() throws IOException {
//1、实例化File类的对象,指明操作文件
File file = new File("hello.txt");
//2、提供具体操作
FileReader fileReader = new FileReader(file);
//3、数据的读入
//read():返回一个字符,如果返回-1,说明已经读完
int read ;
while ((read=fileReader.read())!=-1){
System.out.print((char) read);
}
//4、流的关闭操作
fileReader.close();
}
//对read()操作升级,使用read的重载方法
@Test
public void test2(){
//1、File类的实例化
File file = new File("hello.txt");
//2、FileReader流的实例化
FileReader fr=null;
try {
fr=new FileReader(file);
//3、读入的操作
char[] chars=new char[10];
int ax;
while((ax=fr.read(chars))!=-1){
//注意,i<ax,而不是i<chars.length
for (int i = 0; i < ax; i++) {
System.out.print(chars[i]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//4、关闭流
}
/*
从内存中写出数据到硬盘文件里
输出的操作,file可以不存在
*/
@Test
public void test3(){
//1、提供file类的对象,指明写出到的文件
File file = new File("hello1.txt");
//2、提供FileWriter的对象,用于数据的写出
FileWriter fw=null;
try {
fw=new FileWriter(file);
//加true可以不覆盖原来的文件
//fw=new FileWriter(file,true);
//3、写出操作
fw.write("How do you do.\n");
fw.write("I have a dream");
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
字符流不能用于处理字节文件
*/
@Test
public void test4(){
//1、创建File类的对象,指明读入和写入的文件
File file = new File("hello.txt");
File file1 = new File("hello1.txt");
//2、创建输入流和输出流的对象
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader(file);
fw=new FileWriter(file1);
//3、数据写入和读出操作
char[] chars=new char[5];
int ax;
//把fr里的文件先读入数组中,赋值给fw
while ((ax=fr.read(chars))!=-1){
fw.write(chars,0,ax);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//4、关闭流
}
@Test
public void test5(){
//1、创建File类的对象,指明读入和写入的文件
File file = new File("aifen1.jpg");
System.out.println(file.getAbsolutePath());
File file1 = new File("aifen2.jpg");
//2、创建输入流和输出流的对象
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader(file);
fw=new FileWriter(file1);
//3、数据写入和读出操作
char[] chars=new char[5];
int ax;
//把fr里的文件先读入数组中,赋值给fw
while ((ax=fr.read(chars))!=-1){
fw.write(chars,0,ax);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//4、关闭流
}
}
三、FileInputStream用法
/** * FileInputStream:读取txt内容 * FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。 * */
public class FileInputStreamTest {
// public static void main(String[] args) throws Exception {
//方式一
@Test
public void test1() throws IOException {
//1、通过File类对象
File file = new File("D:/a.txt");
//2、创建FileInputStream类的对象
FileInputStream ifs = new FileInputStream(file);
//3、读取数据
// int num = ifs.read();
// System.out.println((char)num);
//3、循环读取数据
int num;
while ((num = ifs.read()) != -1) {
//循环判断的时候就已经开始自加一了
// int numbe=ifs.read();
// System.out.println(numbe);
System.out.print((char) num);
}
}
//方式二:
@Test
public void test2() throws IOException {
//创建对象
FileInputStream fileInputStream=new FileInputStream("D:/a.txt");
//声明数组,长度为1024
byte[] bytes =new byte[1024];
//把读取的数据保存才数组中,然后把长度赋值给num
int num=fileInputStream.read(bytes);
//输出保存数据的长度
System.out.println(num);//8
//遍历数组
// for (int i = 0; i < bytes.length; i++) {
// System.out.print((char) bytes[i]);//asdfghjk ………………长度为1024,值取完后剩下的都是空格
// }
for (int i = 0; i < num; i++) {
System.out.print((char) bytes[i]);//asdfghjk
}
//关闭流:close
fileInputStream.close();
}
}
四、FileOutputStream用法
/** * FileOutputStream:向文件写入数据 * FileOutputStream(文件地址,false(默认)/true) * false:会覆盖原文中所有内容 true:不会覆盖原文中所有内容 * */
public class OutputStreamTest {
@Test
public void test1() throws Exception {
//如果不写true,默认false,会覆盖原文中所有的文字
FileOutputStream fo=new FileOutputStream("D:/a.txt",true);
fo.write(65);
System.out.println("数据写入完成");
//判断是否运行
if (fo != null) {
fo.close();
}
}
@Test
public void test2(){
FileOutputStream fileOutputStream=null;
try {
fileOutputStream=new FileOutputStream("D:/a.txt",true);
String str="asdfghjsdfghjk";
//字符串转换为bytes数组
byte[] bytes=str.getBytes();
fileOutputStream.write(bytes);
System.out.println("书写成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fileOutputStream!=null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
把数据从一个文本文件中读取,然后输入另一个文件中
FileInputStream and FileOutputStream结合使用
*/
@Test
public void test3(){
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream("D:/a.txt");
fileOutputStream=new FileOutputStream("D:/b.txt",true);
//定义一个外不变量,通过while循环可以不断的加一,获取数据,然后赋值
int num;
while ((num=fileInputStream.read())!=-1){
//char有没有不影响结果
// fileOutputStream.write((char)num);
fileOutputStream.write(num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、Reader类
/** *Reader类: * InputStreamReader: * * FileReader: * file.encoding:查看编码格式 * */
public class InputStremReaderTest {
/*
FileReader(File file)
*/
@Test
public void test() {
InputStreamReader inputStreamReader = null;
FileInputStream fileInputStream =null;
try {
fileInputStream = new FileInputStream("D:/a.txt");
//按照默认的编码格式来读取
inputStreamReader=new InputStreamReader(fileInputStream);
//按照指定的编码格式来读取数据
// inputStreamReader=new InputStreamReader(fileInputStream,"UTF-8");
//读取数据
// int read = inputStreamReader.read();
// System.out.println(read);
//方式一
// int num;
// while ((num=inputStreamReader.read())!=-1){
// System.out.print((char) num);
// }
//方式二
char[] chars=new char[1024];
int num1=inputStreamReader.read(chars);
System.out.println(num1);
for (int i = 0; i < num1; i++) {
System.out.print(chars[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(inputStreamReader!=null) {
inputStreamReader.close();
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
FileReader(String name)
FileReader类对象只能按照本地平台的编码格式读取文件,如果文件和本地的编码格式不一致
可能会出现乱码
*/
@Test
public void test1() {
FileReader fileReader=null;
try {
fileReader=new FileReader("D:/a.txt");
char[] chars=new char[1024];
//读取数据
int num=fileReader.read(chars);
for (int i = 0; i < num; i++) {
System.out.print(chars[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

本文详细介绍了Java中的IO流操作,包括FileInputStream/FileOutputStream和FileReader/FileWriter的基本使用方法及注意事项。通过实例展示了如何读写文本文件和图片文件,并对字节流和字符流的用法进行了对比。

被折叠的 条评论
为什么被折叠?



