国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java 將字符串、list 寫(xiě)入到文件,并讀取內(nèi)容的案例

瀏覽:4日期:2022-08-24 09:51:13

我就廢話(huà)不多說(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 { /** * 字符流寫(xiě)入字符串到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 blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} /** * 設(shè)置編碼格式寫(xiě)出到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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} /** * 追加寫(xiě)入到txt * * @param path * @param data */@SuppressWarnings('resource')public static void FileString4(String path, String data) {try {FileOutputStream outputStream = new FileOutputStream(path, true);// 追加寫(xiě)入outputStream.write(('rn' + data).getBytes());outputStream.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} /** * bitmap 寫(xiě)入到本地 * * @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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 + 'rn');}reader.close();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (StreamCorruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.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+'rn');} bufferedReader.close();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return buffer.toString();}}

補(bǔ)充知識(shí):java讀取txt文件為L(zhǎng)ist

文件在桌面放著名字為hello.txt,先看一下要讀取的內(nèi)容

java 將字符串、list 寫(xiě)入到文件,并讀取內(nèi)容的案例

這是為了方便展示demo隨便寫(xiě)的,格式是一行一個(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:UsersLenovoDesktophello.txt'); for (String s : fileContent) { System.out.println(s); } } }

輸出:

firstsecondThirdFourthFifth

注意:

1.這里File這個(gè)類(lèi)導(dǎo)入的包是Io的,不是Nio的

2. ReaderFileLine.getFileContent('C:UsersLenovoDesktophello.txt'); 這個(gè)路徑是絕對(duì)路徑

3.路徑是一個(gè) 反斜杠 但是在代碼里面反斜杠是轉(zhuǎn)義的意思,所以需要再加一個(gè),如果你是用的IDEA恭喜你,它會(huì)自動(dòng)給你加上

以上這篇java 將字符串、list 寫(xiě)入到文件,并讀取內(nèi)容的案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
主站蜘蛛池模板: 精品国产91久久久久久久 | 99久久免费观看 | 国产免费午夜a无码v视频 | 手机在线看福利 | 亚洲国产人成中文幕一级二级 | 一级美国片免费看 | 精品国产精品久久一区免费式 | 美欧毛片| 国产精品一区伦免视频播放 | 亚洲欧美日韩国产精品26u | 成人看片黄a免费 | 亚洲综合第一欧美日韩中文 | 经典香港a毛片免费观看 | 国产91无套剧情在线播放 | 99久在线 | 国产综合精品在线 | 国产美女主播一级成人毛片 | 一级一级 a爱片免费视频 | 手机看成人免费大片 | 成人网在线视频 | 国产精品一久久香蕉国产线看 | 亚洲成在人 | 97免费在线观看视频 | 网站三级| 国产欧美久久精品 | 国产一级aaaaa毛片欧美 | 大臿蕉香蕉大视频成人 | 亚洲国产成人精彩精品 | 美女被免费视频的网站 | 在线视频中文字幕 | 欧美成人免费全部观看天天性色 | 午夜怡红院| 米奇777第四久久久99 | 日本精品视频在线播放 | 91精品国产爱久久久久久 | 亚洲成在线观看 | 在线高清免费爱做网 | 日本一级毛片高清免费观看视频 | 久久香蕉国产线看观看精品yw | 欧美真人视频一级毛片 | 成人一级网站 |