亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

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

我就廢話不多說了,大家還是直接看代碼吧~

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 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();}} /** * 設置編碼格式寫出到txt * * @param path * @param data */public static void FileString3(String path, String data) {try {OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), 'UTF-8');// 設置編碼格式writer.write(data);writer.close();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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(('rn' + data).getBytes());outputStream.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} /** * 存儲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();}} /** * 設置編碼格式存儲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 寫入到本地 * * @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ù)設置指定編碼 * * @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();}}

補充知識:java讀取txt文件為List

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

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

這是為了方便展示demo隨便寫的,格式是一行一個英文單詞,一共五個。

讀取代碼,這個代碼也是網(wǎng)上找的,忘了哪個博客了。

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這個類導入的包是Io的,不是Nio的

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

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

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

標簽: Java
主站蜘蛛池模板: 日韩欧美中文字幕在线视频 | 1024国产| 免费视频一区二区三区四区 | 在线视频一二三区2021不卡 | 182午夜视频| 国产毛片视频 | 妞干网在线视频 | 麻豆精品视频 | 中文字幕一区在线观看 | 国产小视频精品 | 成人91视频 | 欧美日韩国产在线人成dvd | 日韩欧美中字 | 国产精品国产三级国产普通 | 美女黄网站人色视频免费国产 | 伊人365影院| 免费高清a级毛片在线播放 免费高清小黄站在线观看 免费高清不卡毛片在线看 免费高清毛片 | 99这里只有精品66视频 | 国产成人亚洲综合无 | 成年性视频bbixx | 性做久久久久久久久25的美女 | 中文字幕在线不卡 | 狠狠色丁香婷婷久久综合考虑 | 999www成人免费视频 | 久色一区 | 国产精品成人免费观看 | 欧美亚洲免费久久久 | 精品全国在线一区二区 | 亚洲国产精品久久久久婷婷老年 | 在线a久青草视频在线观看g | 可以免费在线看黄的网站 | 国产福利写真视频在线观看 | 国产伦子一区二区三区 | 99久久国产综合精品女不卡 | wwwww色| 欧美精品午夜毛片免费看 | 久久国产免费一区二区三区 | 亚洲最新视频在线观看 | 欧美日韩亚洲综合另类ac | 国产精品福利在线播放 | 中文国产成人精品久久96 |