文件內(nèi)容操作與實(shí)戰(zhàn)
文件的分類上一篇文章(文件對(duì)象處理)已經(jīng)和大家講解過了。本章主要文件主要針對(duì)于對(duì)文件內(nèi)容的操作展開講解,文件分為:文本文件和二進(jìn)制文件,不同的文件,操作方法也不盡相同。
針對(duì)文本文件,Java提供了一組類——”字符流“,代表類(Reader,Writer)。同樣的,Java也為二進(jìn)制文件提供了一組操作類——”字節(jié)流“,代表類(InputStream,OutputStream)。其實(shí)這兩種的對(duì)文件操作原理是相同的,只是針對(duì)的文件不同,所以才有了字符流和字節(jié)流之分。因?yàn)橛辛俗止?jié)流與字符流之分,所以相對(duì)應(yīng)的,字節(jié)流最低讀寫字符是”一個(gè)字節(jié)“,字符流也有對(duì)應(yīng)限制,字符流最低讀寫”一個(gè)字符 “。
這種讀寫操作一般也統(tǒng)稱為IO流。IO操作是在CPU上進(jìn)行的,以CPU來看待數(shù)據(jù)的輸入和輸出。
字符流
輸入流:Reader
輸出流:Writer
注意:
使用完文件資源后應(yīng)該關(guān)閉文件資源
Reader
創(chuàng)建對(duì)象及使用:
public static void main(String[] args) throws IOException {//創(chuàng)建一個(gè)字符流操作對(duì)象
try(Reader reader = new FileReader("d:/hello.txt")){
while (true) {
//每次讀取一個(gè)字符
int read = reader.read();
if(read == -1){
break;
}
System.out.print((char)read+" ");
}
}
}
Writer
字符流寫入對(duì)象的創(chuàng)建及使用:
同樣的需要使用子類來構(gòu)造
public static void main(String[] args) throws IOException {
try(Writer writer = new FileWriter("d:/hello.txt")){
writer.write("我愛敲代碼");
}
}
文件使用完,需要進(jìn)行關(guān)閉操作,防止不必要的意外發(fā)生。文件的資源泄漏問題不會(huì)立即出現(xiàn),而是一段時(shí)間才會(huì)出現(xiàn)明顯問題。
字節(jié)流
輸入流:inputStream
輸入流:OutputStream
注意:
使用完文件資源后應(yīng)該關(guān)閉文件資源
inputStream
構(gòu)造inputStream對(duì)象
因?yàn)檫@個(gè)類是一個(gè)抽象方法,在構(gòu)造時(shí),不能直接使用類名去調(diào)用。這里使用子類FileInputStream來構(gòu)造對(duì)象。
public static void main(String[] args) throws IOException {
//相當(dāng)于打開一個(gè)文件,將這個(gè)變量與文件關(guān)聯(lián)
InputStream inputStream = new FileInputStream("d:/hello.txt");
inputStream.close();
//每次使用完文件,必須關(guān)閉,防止不必要的資源浪費(fèi)
}
有時(shí)候因?yàn)橹型緍eturn或者結(jié)束了進(jìn)程會(huì)不可避免地?zé)o法將文件關(guān)閉,所以這里使用try/catch/finally語句來進(jìn)行關(guān)閉文件資源操作。
改寫代碼:
public static void main(String[] args) throws IOException {
//方案一
InputStream inputStream = null;
try {
//打開文件
inputStream = new FileInputStream("d:/hello.txt");
}finally {
//關(guān)閉文件
inputStream.close();
}
//方案二,會(huì)自動(dòng)執(zhí)行close關(guān)閉操作
try(InputStream inputStream = new FileInputStream("d:/hello.txt")){
}
}
解決了構(gòu)造函數(shù)接下來我們將繼續(xù)來實(shí)現(xiàn)讀寫操作。
讀操作
可以看見有三個(gè)方法可以進(jìn)行讀操作,接下來我們一一測(cè)試read()方法的作用。
//read()相當(dāng)于把字節(jié)變成二進(jìn)制進(jìn)行表示
public static void main(String[] args) throws IOException {
try(InputStream inputStream = new FileInputStream("d:/hello.txt")){
while (true){
int input = inputStream.read();
//遇到-1則表示沒有數(shù)據(jù),退出循環(huán)
if(input == -1){
break;
}
System.out.println(input);
}
}
}
三個(gè)方法使用的操作基本一致,都是讀到末尾時(shí)返回-1,另外兩個(gè)方法可以指定讀取大小。返回的是總字節(jié)數(shù),不是二進(jìn)制數(shù)據(jù)。
OutputStream
構(gòu)造inputStream對(duì)象
同樣的,這個(gè)也是一個(gè)抽象類,需要調(diào)用子類仿佛來創(chuàng)建對(duì)象:
//創(chuàng)建對(duì)象及使用方法
//將hello寫入文件
public static void main(String[] args) throws IOException {
try(OutputStream outputStream = new FileOutputStream("d:/hello.txt")){
byte[] bytes = {'h','e','l','l','o'};
outputStream.write(bytes);
}
}
實(shí)戰(zhàn)??
遍歷目錄,對(duì)文件進(jìn)行查找或者刪除
1.將輸入目錄進(jìn)行遍歷
2.每次找到一個(gè)文件就獲取目錄
3.對(duì)比文件名與輸入的文件名是否一致
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FindAndDelete {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入要查找的目錄:");
File fileDir = new File(scanner.next());
if(!fileDir.isDirectory()){
System.out.println("您輸入的不是目錄!");
return;
}
System.out.println("請(qǐng)輸入要查找的文件名");
String file = scanner.next();
//遞歸目錄/文件的操作
scanDir(fileDir,file);
}
private static void scanDir(File fileDir, String file) throws IOException {
File[] files = fileDir.listFiles();
if(files == null){
//當(dāng)前目錄為空
return;
}
for(File f:files){
// System.out.println("當(dāng)前搜索到"+f.getCanonicalPath());
if(f.isFile()){
String filename = f.getName();
if(filename.equals(file)){
System.out.println(f.getCanonicalPath()+"存在該文件");
}
}else if(f.isDirectory()){
scanDir(f,file);
}else {
continue;
}
}
}
}
遍歷目錄,查找文件中的內(nèi)容與指定內(nèi)容一致并輸出文件目錄,類似于文件檢索
1.先去簡單遞歸遍歷目錄
2.每次找到一個(gè)文件并獲取目錄
3.在判定的查詢的詞中,是否存在上述內(nèi)容
public class FindTargetDoc {
public static void main(String[] args) throws IOException {
//1.先讓用戶輸入需要搜索文件的根目錄
Scanner scanner = new Scanner(System.in);
System.out.print("請(qǐng)輸入一個(gè)目錄:");
File fileDir = new File(scanner.next());
//判斷輸入的是否為目錄
if(!fileDir.isDirectory()){
System.out.println("輸入錯(cuò)誤,你輸入的目錄不存在");
}
//2.獲取用戶需要查詢的詞
System.out.println("請(qǐng)輸入要查詢的詞");
String word = scanner.next();
//3.遞歸進(jìn)行目錄/文件的操作
scanDir(fileDir,word);
}
private static void scanDir(File fileDir, String word) throws IOException {
//列出當(dāng)前FileDir的內(nèi)容
File[] files = fileDir.listFiles();
if(files == null){
//當(dāng)前目錄是空
return;
}
for (File f:files) {
if(f.isFile()){
//判斷是否為文件
String content = readFile(f);
if(content.contains(word)){
System.out.println(f.getCanonicalPath()+"包含查找的文件");
}
}else if(f.isDirectory()) {
//判斷是否為目錄
scanDir(f,word);
}else {
//不是普通文件也不是目錄文件
continue;
}
}
}
private static String readFile(File f) throws IOException{
//讀取整個(gè)內(nèi)容,返回
//處理是字符流,所以使用字符流進(jìn)行處理
StringBuilder stringBuilder = new StringBuilder();
try(Reader reader = new FileReader(f)){
//將讀取的字符拼接到StringBuilder上
while (true){
int ch = reader.read();
if(ch == -1){
break;
}
stringBuilder.append((char)ch);
}
}
return stringBuilder.toString();
}
}
進(jìn)行普通文件的復(fù)制操作
1.需要準(zhǔn)備兩個(gè)文件
2.一個(gè)有內(nèi)容,一個(gè)無內(nèi)容
3.一個(gè)讀一個(gè)寫,將其讀完存儲(chǔ)在另一個(gè)文檔中即可文章來源:http://www.zghlxwxcb.cn/news/detail-421619.html
public class CopyFile {
public static void main(String[] args) throws IOException {
//這里可以輸入,讀者可以自行更改
File source = new File("d:/aaa/hello.txt");
File dest = new File("d:/aaa/world.txt");
if(!source.exists() || !dest.exists()){
System.out.println("文件不存在");
}
Reader reader = new FileReader(source);
Writer writer = new FileWriter(dest);
StringBuilder stringBuilder = new StringBuilder();
while (true){
int ch = reader.read();
if(ch == -1){
break;
}
stringBuilder.append((char)ch);
}
writer.write(stringBuilder.toString());
reader.close();//記得關(guān)閉資源
writer.close();
}
}
以上就是關(guān)于我們文件內(nèi)容的操作及實(shí)戰(zhàn),感謝閱讀(〃 ̄︶ ̄)人( ̄︶ ̄〃)
文章來源地址http://www.zghlxwxcb.cn/news/detail-421619.html
到了這里,關(guān)于Java文件字符流和字節(jié)流中的實(shí)戰(zhàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!