Java中的IO

java中的流常用来对文件进行读取和写入的操作.


流分为:字节流和字符流

每次开启流用完后都要调用close()关闭流


字节流:(每次都是8个Unicode来操作的流)


输入字节流(InputStream)(读)

FileInputStream继承InputStream,里面有read()方法,只能一个一个字节的读,当有汉字时需要用到read(byte【】)


输出字节流(OutputStream)(写)

FileOutputStream继承OutputStream,里面有write()方法,只能一个一个字节的写,当有汉字时需要用到write(byte【】)



字符流:(每次都是16个Unicode来操作的流)


Reader(读)

里面有FileReader,BufferedReader,

BufferedReader是高级流,也叫缓冲流

里面的方法readLine()


Writer(写)

里面有FileWriter,BufferedReader,

BufferedReader是缓冲流,里面有一个缓冲区,当有内容还没有写入的时候,可以用flush()方法将其赶入写入.

里面的方法writer(),newLine()另起一行



File file = new File("test.txt")

file.createNewFile();//创建一个文件

System.out.println("test.txt文件长度:"+file.length());//获得长度

System.out.println(file.getAbsolutePath());//得到绝对路径

System.out.println(file.getName());//获得文件名 

System.out.println(file.getPath());//得到绝对路径

System.out.println(file.delete()+"删除成功");//文件的删除

file.listFiles()//返回文件中的文件名列表


//输入一个路径可以获得该路径中所有文件夹和文件的名字

public static void getDirName(File files){

if (files.isDirectory()) {

System.out.println("==============="+files.getName()+"文件夹================");

File[] listFiles = files.listFiles();

for (File file : listFiles) {

getDirName(file);

}

}else {

System.out.println(files.getName());

}

}


//用字节流的读写操作

public static void main(String[] args) throws IOException {

//读

File file = new File("abc.txt");

FileInputStream fis = new FileInputStream(file);

byte[] b = new byte[(int) file.length()];

if (fis.read(b) != -1) {

String str = new String(b);

System.out.println(str);

}

fis.close();

//写

File file1 = new File("abc.txt");

FileOutputStream fos = new FileOutputStream(file1,true);//true为追加内容

String str1="我是要被写入的数据";

fos.write(str1.getBytes());//得到字符串每一个字节

fos.close();

}




//用字节流做复制操作

public static void main(String[] args) throws Exception {

File file = new File("test.txt");

File desFile = new File("testCopy.txt");

if (!desFile.exists()) {

desFile.createNewFile();

}

FileInputStream fis = new FileInputStream(file);

FileOutputStream fos = new FileOutputStream(desFile);

byte[] buff = new byte[1024];

int length = 0;

while(length != -1){

fos.write(buff, 0, length);

length = fis.read(buff);

}

fos.close();

fis.close();

}



//字符流做读写操作

               //读

                File file = new File("test.txt");

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String str = br.readLine();

while(str != null){

System.out.println(str);

str = br.readLine();

}

br.close();

fr.close();

//写

File file = new File("test.txt");

FileWriter fw = new FileWriter(file,true);

BufferedWriter bw = new BufferedWriter(fw);

String str  =  "周末到了";

bw.write(str);

bw.close();

fw.close();


//字符流做复制操作

                FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

FileWriter fw1 = new FileWriter(desFile, true);

BufferedWriter bw1 = new BufferedWriter(fw1);

String s2 = br.readLine();

while (s2 != null) {

bw1.write(s2);

s2 = br.readLine();

}

bw1.flush();

System.out.println("复制完成");

bw1.close();

br.close();

fw1.close();

fr.close();

file.delete();


//图片复制的操作

/**

 * @param args

 * @throws IOException 

 * 图片的复制,得用字节流

 * 复制文本文件用字符流

 */

public static void main(String[] args) throws IOException {

File file = new File("abc.jpg");

File desFile = new File("ac.jpg");

if (!desFile.exists()) {

desFile.createNewFile();

}

FileInputStream fis = new FileInputStream(file);

FileOutputStream fos = new FileOutputStream(desFile);

byte[] buff = new byte[(int) file.length()];

int length = 0;

while(length != -1){

fos.write(buff, 0, length);

length = fis.read(buff);

}

System.out.println("图片复制完成");

fos.close();

fis.close();


}



//任意给一个路径,再给一个目的路径,可以讲路径中所有的文件夹和文件复制到目的路径中去

public class CopyFileDirUtil {


static List listDir = new ArrayList();//装文件夹

static List listFile = new ArrayList();//装文件


/**

 * @param args

 * @throws IOException

 * 

 * 文件夹,文件的复制

 */

public static void main(String[] args) throws IOException {

CopyFileDirUtil work04 = new CopyFileDirUtil();

File file = new File("D:/copyTest");


File desFile = new File("D:/desTest");

//获得复制文件夹中的path

getFileDirNames(file);

//遍历创建文件夹

for (int i = 0; i < listDir.size(); i++) {

String s = (String) listDir.get(i);

StringBuffer str = new StringBuffer(s);

str.delete(0, 2);

String copyDir = desFile + "/" + str;

File file2 = new File(copyDir);

if (!file2.exists()) {

file2.mkdir();

}


}


//遍历创建文件,并且复制

for (int j = 0; j < listFile.size(); j++) {

String s1 = (String) listFile.get(j);

StringBuffer str1 = new StringBuffer(s1);

str1.delete(0, 2);

String copyFile = desFile + "/" + str1;

File file3 = new File(copyFile);

if (!file3.exists()) {

file3.createNewFile();

}

File file2 = new File(s1);

FileInputStream fis = new FileInputStream(file2);

FileOutputStream fos = new FileOutputStream(file3);

byte[] b = new byte[(int) file2.length()];

int length = 0;

while (length != -1) {

fos.write(b, 0, length);

length = fis.read(b);

}

fos.close();

fis.close();

}

System.out.println("文件夹及其文件复制成功");


}


//获取将要复制的文件,将文件夹的路径和文件的路径存放到集合中

public static void getFileDirNames(File file) {

if (file.isDirectory()) {

listDir.add(file.getPath());

File[] listFiles = file.listFiles();

for (File file2 : listFiles) {

getFileDirNames(file2);

}

} else {

listFile.add(file.getPath());

}


}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翅膀君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值