Java IO

Java IO

字节(Byte,8位)流Stream,中文占用两个字节

字符流(char) reader,writer,涉及到字符传输的时候就可能需要编码,否则可能出现乱码

字符集(Charset):编码

 

一 字节流

public classReadByteStream {

   public static voidmain(String[] args) {

      try {

         FileInputStream fis = newFileInputStream("hello.txt");

         byte[] input = new byte[22];

         fis.read(input);

         String inputStr = new String(input);

         System.out.println(inputStr);

         fis.close();

      } catch(Exception e) {

         e.printStackTrace();

      }

   }

}

public classWriterByteStream {

   public static voidmain(String[] args) {

      try {

         FileOutputStream fos = newFileOutputStream("hello1.txt");

         String outStr = "write123 写入数据";

         byte[] output = outStr.getBytes("UTF-8");

         fos.write(output);

         fos.close();

      } catch(Exception e) {

         e.printStackTrace();

      }

   }

}

二 通过字节流实现文件的拷贝

public classCopyByByteStream {

   public static voidmain(String[] args) {

      try {

         FileInputStream fis = newFileInputStream("231.png");

         FileOutputStream fos = newFileOutputStream("223.png");

         byte[] input = new byte[50];

         while(fis.read(input) != -1) {

            fos.write(input);

         }

         fos.close();

         fis.close();

         System.out.println("done");

      } catch(Exception e) {

         e.printStackTrace();

      }

   }

}

实现视频拷贝

public class ReadByBufferStream {
public static void main(String[] args) {
try {
long time = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("kero.rmvb");
BufferedInputStream bis = new BufferedInputStream(fis);//(fis,50000000)
FileOutputStream fos = new FileOutputStream("new.rmvb");
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] input = new byte[10000000];
while(bis.read(input) != -1) {
bos.write(input);
}
bis.close();
fis.close();
bos.close();
fos.close();
System.out.println((System.currentTimeMillis() - time)/1000 + "s");
} catch (Exception e) {
e.printStackTrace();
}
}
}

字符流

public class RWByCharStream {
public static void main(String[] args) {
try {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

FileOutputStream fos = new FileOutputStream("newHello.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
char[] input = new char[100];
while(isr.read(input) != -1) {
osw.write(input);
}
osw.close();
fos.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}

//字符流

public class RWByBufferedCharStream {
public static void main(String[] args) {
try {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

FileOutputStream fos = new FileOutputStream("newHello.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(osw);
//BufferedWriter bw = new BufferedWriter(osw);
String input = null;
while((input = br.readLine()) != null) {
//bw.write(input);
pw.println(input);
}
//bw.flush();
pw.flush();//把缓冲写入
pw.close();
//bw.close();
osw.close();
fos.close();
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}

文件读取与写入

public class FileReaderWriter {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("hello.txt");//字符文本
BufferedReader br = new BufferedReader(fr);

FileWriter fw = new FileWriter("new2hello.txt");
BufferedWriter bw = new BufferedWriter(fw);
String line = null;
while((line = br.readLine()) != null) {
bw.write(line + "\n");
}
bw.flush();
bw.close();
fw.close();
br.close();
fr.close();
System.out.println("do");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

RandomAccessFile 读取文件中的特定内容

public class WriteFile extends Thread {
File file;
int block;
public WriteFile(File f, int b) {
this.file = f;
this.block = b;

}
@Override
public void run() {
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.close();
//raf.s
} catch (Exception e) {
}
}
}

使用Apache commons.io库

import org.apache.commons.io.FileUtils;


public class TestFileUtils {
public static void main(String[] args) {
File file = new File("hello.txt");
File file2 = new File("new3hello.txt");
try {
FileUtils.copyFile(file, file2);
} catch (Exception e) {
// TODO: handle exception
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值