這篇文章主要介紹了java中IO實例應用分析的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇java中IO實例應用分析文章都會有所收獲,下面我們一起來看看吧。
I/O 即輸入Input/ 輸出Output的縮寫,其實就是計算機調度把各個存儲中(包括內存和外部存儲)的數據寫入寫出
java中用“流(stream)”來抽象表示這么一個寫入寫出的功能,封裝成一個“類”,都放在java.io這個包里面。
java.io包下提供了各種“流”類和接口,用以獲取不同種類的數據,并 通過標準的方法輸入或輸出數據
程序從內存中讀取數據叫輸入Input。
程序把數據寫入到內存中叫輸出Output。
按操作數據單位不同分為:字節流(8 bit),字符流(16 bit)
按數據流的流向不同分為:輸入流,輸出流
按流的角色的不同分為:節點流,處理流
IO流體系

示例:
public static void main(String[] args) {
iprt();
}
public static void ipst(){
InputStream inputStream = null;
try {
inputStream = new FileInputStream("C:\\1.txt");
int i;
while ( (i = inputStream.read()) != -1){
System.out.print((char) i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}說明:使用InputStream向內存中讀如文件數據。
示例:
public class ImageCopy {
public static void main(String[] args) {
try(
InputStream inputStream = new FileInputStream("D:\\KDA.jpg");
OutputStream outputStream = new FileOutputStream("E:\\aaa\\KDA.jpg")
){
byte[] bytes = new byte[1024];
int i;
while ((i = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}說明:使用輸入流與輸出流結合實現圖片復制的功能。
示例:
public static void iprt(){
Reader reader = null;
try {
reader = new FileReader("C:\\1.txt");
int i ;
while ((i = reader.read()) != -1){
System.out.print((char) i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}說明:使用Reader(字符流)從文件中讀入數據。
public static void iprt(){
Reader reader = null;
Writer writer = null;
try {
reader = new FileReader("C:\\Users\\52425\\Desktop\\1.txt");
writer = new FileWriter("C:\\2.txt");
int i ;
while ((i = reader.read()) != -1){
writer.write(i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}說明:使用字符流實現文件復制功能。
關于“java中IO實例應用分析”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“java中IO實例應用分析”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。