Java io常用知識點

       Java io流知識點
       package myio;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

/**
* 最簡單的 file的
*
* @author Administrator
*
*/
public class test1 {
@Test
public void t1() {

    String parentPath = "E:/壁紙";
    String name = "123.jpg";
    // 相對路徑構建
    File src = new File(parentPath, name);
    System.out.println(src.getName());
    System.out.println(src.getPath());
    // 絕對路徑
    File src2 = new File("E:/壁紙/123.jpg");
    System.out.println(src2.getName());
    System.out.println(src2.getPath());
    // 沒有盤符以當前工作空間爲根目錄
    File src3 = new File("a.txt");
    System.out.println(src3.getName());
    // 如果是絕對路徑就返回絕對路徑 如果是相對路徑就返回相對路徑
    System.out.println(src3.getPath());
    // 絕對路徑
    System.out.println(src3.getAbsolutePath());
    // 獲取上一級目錄
    System.out.println(src.getParent());
    System.out.println(src2.getParent());
    // 沒有盤符創建的文件夾返回null
    System.out.println(src3.getParent());
}

/**
 * 判斷信息
 */
@Test
public void t2() {
    File file = new File("a.txt");
    File src = new File("E:/壁紙/123.jpg");
    System.out.println("判斷文件是否存在:" + file.exists());
    boolean canread = src.canRead();
    boolean canwrite = file.canWrite();
    System.out.println("canread=" + canread);
    System.out.println("canwrite=" + canwrite);
    System.out.println("是否是文件夾:" + src.isDirectory());
    if (src.isFile()) {
        System.out.println("文件");
    } else {
        // 文件夾  或者文件不存在
        if(src.isDirectory()){
            System.out.println("文件夾");
        }else{
            System.out.println("文件不存在");
        }

    }
    // 文件的字節數 只能讀文件的 讀不了文件夾的
    System.out.println(src.length());
}

/**
 * 創建和刪除文件
 * 
 * @throws IOException
 */
@Test
public void t3() throws IOException {
    File src = new File("E:/壁紙/12345.jpg");
    if (!src.exists()) {// 不存在 則創建文件 存在 返回false
        boolean flag = src.createNewFile();
        System.out.println(flag);
    }
    // 刪除文件
    boolean deleteFlag = src.delete();
    System.out.println(deleteFlag);
    // 創建臨時文件
    File file = File.createTempFile("test", ".temp", new File("E:/壁紙"));
    // 退出即刪除
    file.deleteOnExit();
}

}

package myio;

import java.io.File;
import java.io.FileFilter;

import org.junit.Test;

/**
* 操作目錄
*
* @author Administrator
*
*/

public class test2 {
/**
* 創建文件夾
*/
@Test
public void t1() {
String path = “E:/壁紙/123456”;
File file = new File(path);
// 創建文件夾 前提是前面的父目錄存在
boolean a = file.mkdir();
System.out.println(a);
File file2 = new File(“E:/壁紙/123/456/678”);
// 如果父目錄鏈不存在 則一起創建
boolean b = file2.mkdirs();
System.out.println(b);
}

/**
 * 獲取文件夾裏的內容
 */
@Test
public void t2() {
    String path = "E:/壁紙";
    File file = new File(path);
    if (file.isDirectory()) {
        // 獲取文件夾裏所有的文件名 文件夾名也有
        String[] fileNames = file.list();
        for (String string : fileNames) {
            System.out.println(string);
        }
        System.out.println("-------分割線------");
        // 獲取文件夾裏的所有文件路徑 文件夾路徑也有
        File[] fil = file.listFiles();
        for (File f : fil) {
            System.out.println(f.getAbsolutePath());
        }

    }
}

}

package myio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

/**
* 文件的讀取 第一個
*
* @author Administrator
*
*/
public class test3 {
@Test
public void t1() {
// 要讀取的文件
String path = “E:/壁紙/a.txt”;
File file = new File(path);
// 選擇流
InputStream ins = null;
try {
ins = new FileInputStream(file);
// 定義一個字節數組
byte[] by = new byte[1];
int len = 0;
/**
* 有點小不明白 這個地方要new String幾次 能接上嗎 接不上 但是能全部輸出 測試用
* 真要讀文本文件用objectInputstream 一行一行讀
*/
int a = 0;
while ((len = ins.read(by)) != -1) {
a++;
String str = new String(by, 0, len);
System.out.println(str);
}
System.out.println(“a==” + a);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉流

        if (ins != null) {
            try {
                ins.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

/**
 * 寫進文件裏 輸出流 最簡單的 測試
 * 
 * @throws IOException
 */
@Test
public void t2() throws IOException {
    String path = "E:/壁紙";
    // 本來這個文件是不存在的 先創建出來再往裏面寫入東西
    File file = new File(path, "a.txt");
    // 選流
    OutputStream ops = new FileOutputStream(file);
    String str = "羌笛何須怨楊柳,春風不度玉門關。";
    // 寫入
    byte[] byt = str.getBytes();
    // 下面兩個一樣
    // ops.write(byt,0,byt.length);
    ops.write(byt);
    // 刷新一下
    ops.flush();
    // 關閉
    ops.close();
}

/**
 * 文件的拷貝
 * 
 * @throws IOException
 */
@Test
public void t3() throws IOException {
    // 源文件 必須存在不然報錯
    File filea = new File("E:/壁紙/a.txt");
    // 目錄文件 目錄文件可以是不存在的 
    File fileb = new File("E:/壁紙/bbb.txt");

/*  // 如果文件已存在 但是是文件夾
    if (fileb.isDirectory()) {
        fileb.delete();
    }
    // 如果不存在 則創建文件
    if (!fileb.exists()) {
        // fileb.mkdirs();// 錯誤 這是創建文件夾
        fileb.createNewFile();// 創建文件
    }*/
    InputStream ins = new FileInputStream(filea);
    OutputStream ops = new FileOutputStream(fileb);
    /**
     * 這裏設置過大  但文件過小 出現的問題不知道影響不影響
     */
    byte[] byt = new byte[1024];
    int len = 0;
    // 一個讀一個寫
    while ((len = ins.read(byt)) != -1) {
        ops.write(byt);

    }
    /**
     * flush寫在裏面好還是外面
     */
    ops.flush();
    // 關閉流
    ops.close();
    ins.close();

}

}


package myio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

/**
* 拷貝文件夾 要用到遞歸 測試用 沒用封裝成工具類 這樣方便看
*
* @author Administrator
*
*/
public class test4 {

public static void main(String[] args) throws IOException {
       File src = new File("E:/壁紙");
       File dest = new File("E:/test");
    File[] fileList = src.listFiles();
    for (File files : fileList) {
        if (files.isDirectory()) {
            // 在目標文件夾中創建改路徑
            dest = new File(dest, src.getName());
            // 在此處這兩個方法的作用是一樣的
            // dest.mkdir();
            dest.mkdirs();
            t1(files,dest);
        }else{

             files.createNewFile();
        }
        t2(files, dest);
    }
}
public static void t1(File src,File dest) throws IOException {


}

/**
 * 只拷貝單個文件 src源文件 dest 目標文件
 * 
 * @throws IOException
 */
public static void t2(File src, File dest) throws IOException {
    InputStream ins = new FileInputStream(src);
    OutputStream ops = new FileOutputStream(dest);

    int len = 0;
    byte[] byt = new byte[1024];
    while ((len = ins.read(byt)) != -1) {
        ops.write(byt);
    }
    ops.flush();
    ops.close();
    ins.close();
}

}

package myio;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;

import org.junit.Test;

/**
* 字符流操作 字符流只能處理文本文件 .txt .html
*
* @author Administrator
*
*/
public class test5 {
/**
* 字符流的讀 初次測試用
*
* @throws IOException
*/
@Test
public void t1() throws IOException {
File file = new File(“E:/壁紙/a.txt”);
// 轉換流 字符轉字節 這個麻煩就爲了使用 bufferedreader的方法
//解碼設置字符集
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), “utf-8”));
// 字符流用的數組要是char數組
// 這裏設置太大會出問題
/*
* char[] cha = new char[1023]; while (reader.read(cha) != -1) { String
* str = new String(cha); System.out.println(str); }
*/
// 使用包裝類的新方法 一行一行讀
// 字節流包裝類沒有提供新方法
String s = null;
while ((s = reader.readLine()) != null) {
String str = new String(s);
System.out.println(str);
}
reader.close();
}

// 字符流的單個文件的拷貝
// 測試用 字符流不會這樣用 應該會用序列化吧
@Test
public void t2() throws IOException {
    File filea = new File("E:/壁紙/a.txt");
    File fileb = new File("E:/壁紙/b.txt");
    Reader reader = new FileReader(filea);
    Writer writer = new FileWriter(fileb);
    /**
     * 這裏設置太大貌似有問題
     */
    int len=0;
    char[] cha = new char[1024*3];
    while ((len=reader.read(cha)) != -1) {
        //建議都如下所寫 
        //上面註釋掉的存在多讀或者少讀的問題  
        //writer.write(cha);
        writer.write(cha,0,len);
    }
    writer.flush();
    reader.close();
    writer.close();

}

}


package myio;

import java.io.UnsupportedEncodingException;

/**
* 編碼與解碼
*
* @author Administrator
*
*/
public class test6 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = “小程序員”;
// 編碼 轉成二進制
// 編碼與解碼的時候最好都設置字符集
byte[] byt = str.getBytes(“utf-8”);
// 解碼
String s = new String(byt, “utf-8”);
System.out.println(s);
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章