日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Day 8

發布時間:2024/1/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Day 8 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

輸入和輸出處理

File類

文件

什么是文件?
相關記錄或放在一起的數據的集合

Java程序如何訪問文件屬性?
通過Java API:java.io.File類

File類訪問文件屬性

?File類的常用方法

方法名稱? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 說明
boolean exists()? ? ? ? ? ? ? ? ? ?判斷文件或者目錄是否存在
boolean isFile()? ? ? ? ? ? ? ? ? ? 判斷是否是文件
boolean isDIrectory? ? ? ? ? ? ? 判斷是否是目錄
String getPath()? ? ? ? ? ? ? ? ? ? 返回此對象表示的文件的相對路徑名
String getAbsolutePath? ? ? ? 返回此對象表示的文件的絕對路徑名
String getName()? ? ? ? ? ? ? ? ? 返回此對象表示的文件或目錄的名稱
boolean delete()? ? ? ? ? ? ? ? ? ?刪除此對象指定的文件或目錄
boolean createNewFiel()? ? ? 創建名稱的空文件,不創建文件夾
long length? ? ? ? ? ? ? ? ? ? ? ? ? ?返回文件的長度,單位為字節,如果文件不存在,則返回0L

什么是流

流是一組有序的數據序列,以先進先出方式發送信息的通道

Java流的分類
按流向區分:
分為輸出流和輸入流,其中輸入流以InputStream和Reader作為基類,輸出流以OutputStream和Writer作為基類。(輸入輸出流是相對于計算機內存來說的)


按照處理數據單元劃分:
分為字節流和字符流,其中字節流又可分為字節輸入流InputStream和字節輸出流OutputStream,字符流分為字符輸入流Reader和字符輸出流Writer。
字節流是8位通用字節流,字符流是16位Unicode字符流。

FileInputStream
InputStream類常用方法

1、int read()
從輸入流一個字節一個字節的讀,返回的是該字節的整數表示形式,如果讀到了輸入流的末尾,返回-1。
2、int read(byte[] b)
從輸入流讀取若干字節,把這些字節保存到字節數組中。返回的是讀取到的字節數,如果讀到了輸入流的末尾,返回-1。
3、int read(byte[] b,int off, int len)
從輸入流讀取若干字節,把這些自己保存到字節數組b中。off指的是字節數組中開始保存數據的起始位置下標。len指讀取的自己數目。返回的是讀取到的字節數,如果讀到了輸入流的末尾,返回-1。
4、void close()
關閉流
5、int available()
可以從輸入流中讀取的字節數目

子類FileInputStream常用的構造方法
1、FileInputStream(File file)
2、FileInputStream(String name)//完整的路徑

使用FileInputStream讀文本文件
步驟:
1、引入相關的類

import java.io.IOException;
import java.io.FileInputStream;

2、構造文件輸入流FileInputStream對象

FileInputStream fis=new FileInputStream("D:/test.txt");

3、讀取文本文件的數據

fis.available();
fis.read();

4、關閉文件流對象

fis.close();

1、借助FileInputStream對象的read()方法讀取文件

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;public class TestFileInputStream {public static void main(String[] args) {FileInputStream fis=null;try {fis=new FileInputStream("D:/test.txt");int data;while((data=fis.read())!=-1){System.out.print((char)data);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {fis.close();} catch (IOException e) {e.printStackTrace();}}} }

2、借助FileInputStream對象的read(byte[] b)方法讀取文件

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;public class TestFileInputStream {public static void main(String[] args) {FileInputStream fis=null;try {fis=new FileInputStream("D:/test.txt");byte[] b=new byte[fis.available()];int data;while((data=fis.read(b))!=-1){for (int i = 0; i < b.length; i++) {System.out.println((char)b[i]);}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {fis.close();} catch (IOException e) {e.printStackTrace();}}} }

OutputStream
OutputStream類常用方法

1、void write(int c)
往輸出流中寫入一個個的字節
2、void write(byte[] b)
往輸出流中寫入一個字節數組
3、void write(byte[] b, int off, int len)
往輸出流中寫入一個字節數組,off代表開始從字節數組的off位置開始往外寫,len代表往外寫len長度的字節
4、void close()
關閉輸出流
5、void flush()
強制把緩沖區中的數據全部寫到輸出流中

子類FileOutputStream常用的構造方法
1、FileOutputStream(File file)
2、FileOutputStream(String name)//路徑
3、FileOutputStream(String name, boolean append) //當append的值為true時,追加寫入

注意:
1、前兩種構造方法在向文件寫數據時將覆蓋文件中的原有內容
2、創建FileOutputStream實例時,如果相應的文件并不存在,則會自動創建一個空的文件

使用FileOutputStream寫入文件
1、引入相關的類

import java.io.IOException;
import java.io.FileOutputStream;

2、構造文件輸出流FileOutputStream對象

FileOutputStream fos=new FileOutputStream("D:/test.txt");

3、把數據寫入文本文件

String str="好好學習 天天向上";
byte[] b=str.getBytes(str);
fos.write(b);

4、關閉文件流對象

fos.close();

完整代碼:

import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class TestFileOutputStream {public static void main(String[] args) {FileOutputStream fos=null;try {fos=new FileOutputStream("D:/test.txt");String str="好好學習 天天向上";byte[] b=str.getBytes();fos.write(b);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}}} }


?

總結

以上是生活随笔為你收集整理的Day 8的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。