一、什么是IO流
輸入流和輸出流。
- 輸入流:數據從數據源(文件)到程序(內存)的路徑
- 輸出流:數據從程序(內存)到數據源(文件)的路徑
二、常用的文件操作
學習目標:創建文件對象相關構造器和方法
new File(String pathname)//根據路徑構建一個File對象
new File(File parent,String child)//根據父目錄文件+子路徑構建
new File(String parent,String child)//根據父目錄+子路徑構建
學習任務:在e盤下,創建文件news1.txt、news2.txt、news3.txt用三種不同方式創建。
三種方式簡單看一下就行,后面會經常遇到。
new File(String pathname)//根據路徑構建一個File對象
package com.file;
import java.io.*;
public class FileCreate {
public static void main(String[] args) {
//方式 1 new File(String pathname)
String filePath = "e:\\news1.txt";
File file = new File(filePath);
try {
//創建新文件
file.createNewFile();
System.out.println("文件創建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
new File(File parent,String child)//根據父目錄文件+子路徑構建
package com.file;
import java.io.*;
public class FileCreate {
public static void main(String[] args) {
//方式 2 new File(File parent,String child) //根據父目錄文件+子路徑構建
//e:\\news2.txt
File parentFile = new File("e:\\");
String fileName = "news2.txt";
//這里的 file 對象,在 java 程序中,只是一個對象
//只有執行了 createNewFile 方法,才會真正的,在磁盤創建該文件
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("創建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
}
new File(String parent,String child)//根據父目錄+子路徑構建
package com.file;
import java.io.*;
public class FileCreate {
public static void main(String[] args) {
//方式 3 new File(String parent,String child) //根據父目錄+子路徑構建
String parentPath = "e:\\";
String fileName = "news3.txt";
File file = new File(parentPath, fileName);
try {
file.createNewFile();
System.out.println("創建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、獲取文件的相關信息
getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
學習任務:獲取文件的大小、文件名、路徑、父File,是文件還是目錄(目錄本質也是文件,一種特殊的文件),是否存在
四、目錄的操作和文件刪除
學習任務:
- 判斷e:\news1.txt是否存在,如果存在就刪除
- 判斷e:\\demo02是否存在,存在就刪除,否則提示不存在
- 判斷e:\\demo\a\b\c目錄是否存在,如果存在就提示已經存在,否則就創建
package com.collection;
import java.io.File;
public class Delete {
public static void main(String[] args) {
String filePath="e:\\news1.txt";
File file=new File(filePath);
if(file.exists()){
file.delete();
}else {
System.out.println("否則文件不存在~");
}
}
}
package com.collection;
import java.io.File;
public class Delete {
public static void main(String[] args) {
String filePath="e:\\demo02";
File file=new File(filePath);
if(file.exists()){
if(file.delete()){
System.out.println(filePath+"刪除成功");
}
else{
System.out.println(filePath+"刪除失敗");
}
}else {
System.out.println("否則目錄不存在~");
}
}
}
package com.collection;
import java.io.*;
public class Delete {
public static void main(String[] args) {
String directoryPath="e:\\demo\\a\\b\\c";
File file=new File(directoryPath);
if(file.exists()) {
System.out.println(directoryPath + "存在。。");
}
else {
if (file.mkdirs()){
System.out.println(directoryPath+"創建成功。。");
}else{
System.out.println(directoryPath+"創建失敗。。");
}
}
}
}
按操作數據單位不同分為:字節流(8bit)二進制文件,字符流(按字符)文本文件
按數據流的流向不同分為:輸入流,輸出流
按流的角色的不同分為:節點流,處理流/包裝流
字節流:InputStream,OutputStream
字符流:Reader,Writer
五、IO流體系圖-常用的類
六、FileInputStream常用方法
學習任務:請使用 FileInputStream 讀取 hello.txt 文件,并將文件內容顯示
先在e盤下創建hello.txt輸入內容hello world
package com.FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
//字節流文件的輸入程序
public class FileInputStream_ {
public static void main(String[] args) {
String filePath="e:\\hello.txt";
int readData=0;
FileInputStream fileInputStream=null;
try {
//創建 FileInputStream 對象,用于讀取文件
fileInputStream=new FileInputStream(filePath);
//從該輸入流讀取一個字節的數據。 如果沒有輸入可用,此方法將阻止。
//如果返回-1 , 表示讀取完畢
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char)readData);//轉成 char 顯示
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//關閉文件流
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
七、FileOutputStream常用方法
學習任務:請使用 FileOutputStream 在 abc.txt 文件,中寫入 “hello,world”. 如果文件不存在,會創建 文件(注意:前提是目錄已經存在.)
package com.hspedu.outputstream_;
import java.io.*;
public class FileOutputStream01 {
public static void main(String[] args) {
String filePath = "D:\\abc.txt";
FileOutputStream fileOutputStream = null;
try {
//得到 FileOutputStream 對象 對象
//1. new FileOutputStream(filePath) 創建方式,當寫入內容是,會覆蓋原來的內容
// 2. new FileOutputStream(filePath, true) 創建方式,當寫入內容是,是追加到文件后面
fileOutputStream = new FileOutputStream(filePath);
//寫入一個字節
//fileOutputStream.write('H');
//寫入字符串
String str = "hello,world!";
//str.getBytes() 可以把 字符串-> 字節數組
//fileOutputStream.write(str.getBytes());
/*
write(byte[] b, int off, int len) 將 len 字節從位于偏移量 off 的指定字節數組寫入此文件輸出流
*/
fileOutputStream.write(str.getBytes(), 0, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
學習任務:編程完成圖片/音樂 的拷貝
package com.hspedu.outputstream_;
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
String srcFilePath="e:\\9030.jpg";
String destFilePath="e:\\9031.jpg";
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream(srcFilePath);
fileOutputStream=new FileOutputStream(destFilePath);
//定義一個字節數組,提高讀取效果
byte[] buf=new byte[1024];
int readLen=0;
while ((readLen=fileInputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷貝成功!");
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(fileInputStream!=null){
fileInputStream.close();
}
if(fileOutputStream!=null){
fileInputStream.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
八、FileReader常用方法
- new FileReader(File/String)
- read:每次讀取單個字符,返回該字符,如果到文件末尾返回-1
- read(char[]):批量讀取多個字符到數組,返回讀取到的字符數,如果到文件末尾返回-1
- 相關API:
- new String(char[]):將char[]轉換成String
- new String(char[],off,len):將char[]的指定部分轉換成String
九、FileWriter常用方法
- new FileWriter(File/String):覆蓋模式,相當于流的指針在首端
- new FileWriter(File/String,true):追加模式,相當于流的指針在尾端
- write(int):寫入單個字符
- write(char[]):寫入指定數組
- write(char[],off,len):寫入指定數組的指定部分
- write(string):寫入單個字符
- write(string[],off,len):寫入字符串的指定部分
- 相關API:String類:toCharArray:將String轉換成char[]
- FileWriter使用后,必須要關閉(close)或刷新(flush), 否則寫入不到指定的文件!
學習任務:使用 FileReader 從 story.txt ,這一步先在story.txt存在數據,然后在端口輸出數據顯示出來
package com.reader_;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile01 {
public static void main(String[] args) {
String filePath="e:\\story.txt";
FileReader fileReader=null;
int data=0;
//創建FileReader對象
try{
fileReader =new FileReader(filePath);
//循環讀取 使用 read, 單個字符讀取
while((data=fileReader.read())!=-1){
//data數值為整數型,強制轉換為字符
System.out.print((char)data);
}
}catch(
IOException e){
e.printStackTrace();
}finally {
try{
if(fileReader!=null){
//關閉文件流
fileReader.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
學習任務:字符數組讀取文件
package com.reader_;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile02 {
public static void main(String[] args) {
String filePath="e:\\story.txt";
FileReader fileReader=null;
int readLen=0;
char[] buf=new char[8];
//創建FileReader對象
try{
fileReader =new FileReader(filePath);
//循環讀取 使用 read(buf), 返回的是實際讀取到的字符數
//如果返回-1, 說明到文件結束
while((readLen=fileReader.read(buf))!=-1){
System.out.print(new String(buf,0,readLen));
}
}catch(
IOException e){
e.printStackTrace();
}finally {
try{
if(fileReader!=null){
fileReader.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
學習任務:使用 FileWriter 將 “風雨之后,定見彩虹” 寫入到 note.txt 文件中
package com.hspedu.writer_;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriter_ {
public static void main(String[] args) {
String filePath="e:\\noth.txt";
FileWriter fileWriter=null;
char[] chars={'a','b','c'};
try {
fileWriter = new FileWriter(filePath);
//3) write(int):寫入單個字符
fileWriter.write('H');
// 4) write(char[]):寫入指定數組
fileWriter.write(chars);
// 5) write(char[],off,len):寫入指定數組的指定部分
fileWriter.write("程序員飛鳥".toCharArray(), 0, 3);
// 6) write(string):寫入整個字符串
fileWriter.write(" 你好廣州");
fileWriter.write("風雨之后,定見彩虹");
// 7) write(string,off,len):寫入字符串的指定部分
fileWriter.write("上海天津", 0, 2);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//對應 FileWriter , 一定要關閉流,或者 flush 才能真正的把數據寫入文件里面
fileWriter.flush();//關閉文件流
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("程序結束~");
}
}
到此這篇關于Java細數IO流基礎到方法使用的文章就介紹到這了,更多相關Java IO流內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!