字節流java
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String path = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- FileOutputStream o = null;
- try {
- o = new FileOutputStream(path);
- String str = "Nerxious is a good boy\r\n";
- byte[] buf = str.getBytes();
- //也能夠直接使用o.write("String".getBytes());
- //由於字符串就是一個對象,能直接調用方法
- o.write(buf);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(o != null) {
- try {
- o.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String path = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- FileInputStream i = null;
- try {
- i = new FileInputStream(path);
- //方式一:單個字符讀取
- //須要注意的是,此處我用英文文本測試效果良好
- //但中文就悲劇了,不過下面兩個方法效果良好
- int ch = 0;
- while((ch=i.read()) != -1){
- System.out.print((char)ch);
- }
- //方式二:數組循環讀取
- /*
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = i.read(buf)) != -1) {
- System.out.println(new String(buf,0,len));
- }
- */
- //方式三:標準大小的數組讀取
- /*
- //定一個一個恰好大小的數組
- //available()方法返回文件的字節數
- //可是,若是文件過大,內存溢出,那就悲劇了
- //因此,親們要慎用!!!上面那個方法就不錯
- byte[] buf = new byte[i.available()];
- i.read(buf);
- //由於數組大小恰好,因此轉換爲字符串時無需在構造函數中設置起始點
- System.out.println(new String(buf));
- */
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(i != null) {
- try {
- i.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String bin = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "一我的生活.mp3";
- String copy = File.separator + "home" + File.separator + "siu" +
- File.separator + "life" + File.separator + "一我的生活.mp3";
- FileInputStream i = null;
- FileOutputStream o = null;
- try {
- i = new FileInputStream(bin);
- o = new FileOutputStream(copy);
- //循環的方式讀入寫出文件,從而完成複製
- byte[] buf = new byte[1024];
- int temp = 0;
- while((temp = i.read(buf)) != -1) {
- o.write(buf, 0, temp);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(i != null) {
- try {
- i.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(o != null) {
- try {
- o.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String bin = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "一我的生活.mp3";
- String copy = File.separator + "home" + File.separator + "siu" +
- File.separator + "life" + File.separator + "一我的生活.mp3";
- FileInputStream i = null;
- FileOutputStream o = null;
- BufferedInputStream bi = null;
- BufferedOutputStream bo = null;
- try {
- i = new FileInputStream(bin);
- o = new FileOutputStream(copy);
- bi = new BufferedInputStream(i);
- bo = new BufferedOutputStream(o);
- byte[] buf = new byte[1024];
- int temp = 0;
- while((temp = bi.read(buf)) != -1) {
- bo.write(buf,0,temp);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(bi != null) {
- try {
- i.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(bo != null) {
- try {
- o.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }