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());}/** * 判斷資訊 */@Testpublic 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 */@Testpublic 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);
}
/** * 擷取檔案夾裡的內容 */@Testpublic 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 */@Testpublic 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 */@Testpublic 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();
}
// 字元流的單個檔案的拷貝// 測試用 字元流不會這樣用 應該會用序列化吧@Testpublic 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);
}
}