java 將字符串、list 寫入到文件,并讀取內(nèi)容的案例
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.StreamCorruptedException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import android.graphics.Bitmap;
public class FileUtils {
/**
* 字符流寫入字符串到txt
*/
@SuppressWarnings("resource")
public static void FileString(String path, String data) {
try {
FileWriter writer = new FileWriter(path);// 字符流
writer.write(data);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字節(jié)輸出到txt
*
* @param path
* @param data
*/
@SuppressWarnings("resource")
public static void FileString2(String path, String data) {
try {
FileOutputStream outputStream = new FileOutputStream(path);// 字節(jié)流
outputStream.write(data.getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 設(shè)置編碼格式寫出到txt
*
* @param path
* @param data
*/
public static void FileString3(String path, String data) {
try {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");// 設(shè)置編碼格式
writer.write(data);
writer.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 追加寫入到txt
*
* @param path
* @param data
*/
@SuppressWarnings("resource")
public static void FileString4(String path, String data) {
try {
FileOutputStream outputStream = new FileOutputStream(path, true);// 追加寫入
outputStream.write(("\r\n" + data).getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 存儲(chǔ)list到文件
*
* @param path
* @param list
*/
@SuppressWarnings("resource")
public static <T> void FileWriteList1(String path, List<T> list) {
try {
FileOutputStream outputStream = new FileOutputStream(path);
ObjectOutputStream stream = new ObjectOutputStream(outputStream);
stream.writeObject(list);
stream.close();
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 設(shè)置編碼格式存儲(chǔ)list到txt
*
* @param path
* @param list
*/
@SuppressWarnings("resource")
public static <T> void FileWriteList(String path, List<T> list) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
for (T s : list) {
bufferedWriter.write(s.toString());
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedWriter.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* bitmap 寫入到本地
*
* @param path
* @param bitmap
*/
@SuppressWarnings("resource")
public static void FileBitmap(String path, Bitmap bitmap) {
try {
FileOutputStream outputStream = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 讀取本地文件數(shù)據(jù)設(shè)置指定編碼
*
* @param path
*/
@SuppressWarnings("resource")
public static String FileInputString(String path) {
StringBuffer buffer = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String data = null;
while ((data = reader.readLine()) != null) {
buffer.append(data + "\r\n");
}
reader.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer.toString();
}
/**
* 根據(jù)字節(jié)讀取文件
*
* @param path
* @return
*/
@SuppressWarnings("resource")
public static String FileInputString2(String path) {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream inputStream = new FileInputStream(path);
byte[] bytes = new byte[1024];
int bytead = 0;
while ((bytead = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, bytead));
}
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer.toString();
}
/**
* 獲取本地文件中的list
*
* @param path
*/
@SuppressWarnings("resource")
public static <T> void FileInputList(String path) {
try {
FileInputStream inputStream = new FileInputStream(path);
ObjectInputStream stream = new ObjectInputStream(inputStream);
List<T> list = (List<T>) stream.readObject();
inputStream.close();
stream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 高效讀取指定編碼格式的文件
* @param path
* @return
*/
@SuppressWarnings("resource")
public static String FileInput3(String path) {
StringBuffer buffer = new StringBuffer();
try {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream(path), "UTF-8"));
String data = null;
while ((data = bufferedReader.readLine()) != null) {
buffer.append(data+"\r\n");
}
bufferedReader.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer.toString();
}
}
補(bǔ)充知識(shí):java讀取txt文件為L(zhǎng)ist
文件在桌面放著名字為hello.txt,先看一下要讀取的內(nèi)容

這是為了方便展示demo隨便寫的,格式是一行一個(gè)英文單詞,一共五個(gè)。
讀取代碼,這個(gè)代碼也是網(wǎng)上找的,忘了哪個(gè)博客了。
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author :
* @date : 2018/8/30
* @description:
*/
public class ReaderFileLine {
/**
* @author:
* @date:2018/8/30
* @description:從txt文件讀取List<String>
*/
public static List<String> getFileContent(String path) {
List<String> strList = new ArrayList<String>();
File file = new File(path);
InputStreamReader read = null;
BufferedReader reader = null;
try {
read = new InputStreamReader(new FileInputStream(file),"utf-8");
reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
strList.add(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return strList;
}
public static void main(String[] args) {
List<String> fileContent =
ReaderFileLine.getFileContent("C:\\Users\\Lenovo\\Desktop\\hello.txt");
for (String s : fileContent) {
System.out.println(s);
}
}
}
輸出:
first
second
Third
Fourth
Fifth
注意:
1.這里File這個(gè)類導(dǎo)入的包是Io的,不是Nio的
2. ReaderFileLine.getFileContent("C:\\Users\\Lenovo\\Desktop\\hello.txt"); 這個(gè)路徑是絕對(duì)路徑
3.路徑是一個(gè) 反斜杠 \ 但是在代碼里面反斜杠是轉(zhuǎn)義的意思,所以需要再加一個(gè)\,如果你是用的IDEA恭喜你,它會(huì)自動(dòng)給你加上
以上這篇java 將字符串、list 寫入到文件,并讀取內(nèi)容的案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- java將String字符串轉(zhuǎn)換為L(zhǎng)ist<Long>類型實(shí)例方法
- Java中ArrayList去除重復(fù)元素(包括字符串和自定義對(duì)象)
- java的Jackson將json字符串轉(zhuǎn)換成泛型List
- 淺談java 字符串,字符數(shù)組,list間的轉(zhuǎn)化
- Java中Json字符串直接轉(zhuǎn)換為對(duì)象的方法(包括多層List集合)
- JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換
- Java8 將List轉(zhuǎn)換為用逗號(hào)隔開(kāi)的字符串的多種方法
相關(guān)文章
RabbitMQ 的七種隊(duì)列模式和應(yīng)用場(chǎng)景
最近學(xué)習(xí)RabbitMQ,本文就記錄一下RabbitMQ 的七種隊(duì)列模式和應(yīng)用場(chǎng)景,方便以后使用,也方便和大家共享,相互交流2021-05-05
MyBatisPlus中使用or()和and()遇到的問(wèn)題及細(xì)節(jié)處理
這篇文章主要介紹了MyBatisPlus中使用or()和and()遇到的問(wèn)題,本文通過(guò)多種寫法實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
通過(guò)字節(jié)碼看java中this的隱式傳參詳解
這篇文章主要給大家介紹了關(guān)于如何通過(guò)字節(jié)碼看java中this的隱式傳參的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Apache Commons Math3探索之多項(xiàng)式曲線擬合實(shí)現(xiàn)代碼
這篇文章主要介紹了Apache Commons Math3探索之多項(xiàng)式曲線擬合實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10
java中給實(shí)體對(duì)象屬性的空值賦默認(rèn)值
這篇文章主要介紹了java中給實(shí)體對(duì)象屬性的空值賦默認(rèn)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot下載Excel文件時(shí),報(bào)錯(cuò)文件損壞的解決方案
這篇文章主要介紹了SpringBoot下載Excel文件時(shí),報(bào)錯(cuò)文件損壞的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

