標籤:檔案的 dir end static 輸出 儲存 流轉 src 位元組
/*//建立一個新檔案public static void main(String[] args) {File file=new File("D:\\hello.txt");try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}*//*//File類的兩個常量 public static void main(String[] args) {System.out.println(File.separator);//結果: System.out.println(File.pathSeparator);//結果: :}*//*//刪除一個檔案public static void main(String[] args) {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);if(file.exists()){file.delete();}else {System.out.println("檔案不存在!");}}*//*//建立一個檔案夾public static void main(String[] args) {String fileName="D:"+File.separator+"hello";File file=new File(fileName);file.mkdirs();}*//*//使用listFiles列出指定目錄的全部檔案 //* listFiles輸出的是完整路徑public static void main(String[] args) {String fileName="D:"+File.separator;File file=new File(fileName);File[] str=file.listFiles();for(int i=0;i<str.length;i++){System.out.println(str[i]);}}*//*使用isDirectory判斷一個指定的路徑是否為目錄*//*public static void main(String[] args) {String fileName="D:"+File.separator;File file=new File(fileName);if(file.isDirectory())System.out.println("yes");else {System.out.println("no");}}*//*列出指定目錄的全部內容*//*public static void main(String[] args) {String fileName="D:"+File.separator;File file=new File(fileName);print(file);}public static void print(File f){if(f!=null){if(f.isDirectory()){File[] fileArray=f.listFiles();if(fileArray!=null){for(int i=0;i<fileArray.length;i++){print(fileArray[i]);//遞迴調用}}}else {System.out.println(f);}}}*//*使用RandomAccessFile寫入檔案public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);RandomAccessFile randomAccessFile=new RandomAccessFile(file, "rw");randomAccessFile.writeBytes("1234567890");randomAccessFile.writeInt(42);randomAccessFile.writeBoolean(true);randomAccessFile.writeChar(88);randomAccessFile.writeDouble(12.678);randomAccessFile.writeFloat(23.5f);randomAccessFile.close();}//如果你此時開啟hello。txt查看的話,會發現那是亂碼。*//*位元組流 * 向檔案中寫入字串public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);OutputStream outputStream=new FileOutputStream(file);String string="Hello";byte[] bs=string.getBytes();outputStream.write(bs);outputStream.close();}*//** * 位元組流 * 向檔案中一個位元組一個位元組的寫入字串 * @throws IOException * *//*public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);OutputStream outputStream=new FileOutputStream(file);String string="Hello GG";byte[] bs=string.getBytes();for(int i=0;i<bs.length;i++){outputStream.write(bs[i]);}outputStream.close();}*//** * 位元組流 * 向檔案中追加新內容: * @throws IOException * *//*public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);OutputStream outputStream=new FileOutputStream(file,true);String string="Hello MM";//String str="\r\nRollen"; 可以換行byte[] bs=string.getBytes();for(int i=0;i<bs.length;i++){outputStream.write(bs[i]);}outputStream.close();}*//** * 位元組流 * 讀檔案內容 * @throws IOException * *//*public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);InputStream inputStream=new FileInputStream(file);//byte[] bs=new byte[1024];//預先申請了一個指定大小的空間byte[] bs=new byte[(int) file.length()];inputStream.read(bs);System.out.println("檔案長度為:"+file.length());inputStream.close();System.out.println(new String(bs));}*//** * 位元組流 *讀檔案 * @throws IOException * *//*public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);InputStream inputStream=new FileInputStream(file);byte[] bs=new byte[1024];int count=0;int temp=0;while((temp=inputStream.read())!=(-1)){//當讀到檔案末尾的時候會返回-1.正常情況下是不會返回-1的bs[count++]=(byte) temp;}inputStream.close();System.out.println(new String(bs));}*//** * 字元流 * 寫入資料 * @throws IOException * *//*public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);Writer out=new FileWriter(file);String string="朋友";out.write(string);out.close();}*//** * 字元流 * 從檔案中讀出內容 * @throws IOException *採用迴圈讀取的方式,因為我們有時候不知道檔案到底有多大。 * *//*public static void main(String[] args) throws IOException {String fileName="D:"+File.separator+"hello.txt";File file=new File(fileName);char[] ch=new char[100];Reader reader=new FileReader(file);int temp=0;int count=0;while((temp=reader.read())!=(-1)){ch[count++]=(char) temp;}reader.close();System.out.println(new String(ch, 0, count));}*/
/**
* 將位元組輸出資料流轉化為字元輸出資料流
* @throws IOException
* */
/*public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File file=new File(fileName);
Writer out=new OutputStreamWriter(new FileOutputStream(file));
out.write("MM");
out.close();
}*/
/**
* 將位元組輸入資料流變為字元輸入資料流
* @throws IOException
* */
/*public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File file=new File(fileName);
Reader reader=new InputStreamReader(new FileInputStream(file));
char[] cs=new char[100];
int len=reader.read(cs);
System.out.println(new String(cs, 0, len));
reader.close();
}*/
/**
* 使用內容操作流將一個大寫字母轉化為小寫字母
* @throws IOException
* 內容操作流一般用來產生一些臨時資訊的,這樣可以避免刪除的麻煩。
* */
/*public static void main(String[] args) throws IOException {
String str="ADFGHJKLUYTG";
ByteArrayInputStream inputStream=new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int temp=0;
while((temp=inputStream.read())!=(-1)){
char ch=(char) temp;
outputStream.write(Character.toLowerCase(ch));
}
String outStr= outputStream.toString();
inputStream.close();
outputStream.close();
System.out.println(outStr);
}*/
1、位元組流和字元流的區別:
位元組流在操作的時候本身是不會用到緩衝區的,是檔案本身的直接操作的,但是字元流在操作的 時候,是會用到緩衝區的,是通過緩衝區來操作檔案的。
使用位元組流好還是字元流好呢?
答案是位元組流。首先因為硬碟上的所有檔案都是以位元組的形式進行傳輸或者儲存的,包括圖片等內容。但是字元只是在記憶體中才會形成的,所以在開發中,位元組流使用廣泛。
2、檔案的複製:
方法一:DOS下就有一個檔案複製功能
方法二:用程式
/** * 檔案的複製 * @throws IOException * 思路:從一個檔案中讀取內容,邊讀邊寫入另一個檔案。 * */public static void main(String[] args) throws IOException {String helloFileName="D:"+File.separator+"hello.txt";String rollenFileName="D:"+File.separator+"rollen.txt";File file1=new File(helloFileName);File file2=new File(rollenFileName);if(!file1.exists()){System.out.println("被複製的檔案不存在!");System.out.println(1);}InputStream inputStream=new FileInputStream(file1);OutputStream outputStream=new FileOutputStream(file2);if(inputStream!=null&&outputStream!=null){int temp=0;while((temp=inputStream.read())!=(-1)){outputStream.write(temp);}}inputStream.close();outputStream.close();}
3、管道流
管道流主要用以進行兩個線程之間的通訊。
PipedOutputStream 管道輸出資料流
PipedInputStream 管道輸入資料流
/*訊息發送類*/public class Send implements Runnable{private PipedOutputStream out=null;public Send() {out=new PipedOutputStream();}public PipedOutputStream getOut(){return this.out;}@Overridepublic void run() {String message="hello MM";try {out.write(message.getBytes());} catch (IOException e) {e.printStackTrace();}try {out.close();} catch (IOException e) {e.printStackTrace();}}}/*接受訊息類*/public class Recive implements Runnable{private PipedInputStream input=null;public Recive() {input=new PipedInputStream();}public PipedInputStream getInput(){return this.input;}@Overridepublic void run() {byte[] bs=new byte[1000];int len=0;try {len=this.input.read(bs);} catch (IOException e) {e.printStackTrace();}try {input.close();} catch (IOException e) {e.printStackTrace();}System.out.println("接收的內容是:"+(new String(bs, 0, len)));}}/*測試類別*/public static void main(String[] args) {Send send=new Send();Recive recive=new Recive();try {send.getOut().connect(recive.getInput());//管道串連} catch (IOException e) {e.printStackTrace();}new Thread(send).start();new Thread(recive).start();}}
4、列印流
/* 使用PrintStream進行輸出*//*public static void main(String[] args) throws IOException {PrintStream printStream=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"hello.txt")));printStream.print(true);printStream.print("GG");printStream.close();}*//*使用PrintStream進行輸出 * 並進行格式化*//*public static void main(String[] args) throws IOException {PrintStream printStream=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"hello.txt")));String name="GG";int age=26;printStream.printf("姓名:%s;年齡:%d!",name,age);printStream.close();}*//*使用OutputStream向螢幕上輸出內容*/public static void main(String[] args) {OutputStream out=System.out;try {out.write("MM".getBytes());} catch (IOException e) {e.printStackTrace();}try {out.close();} catch (IOException e) {e.printStackTrace();}}
java之IO整理(上)