一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 將字符串、list 寫入到文件,并讀取內容的案例

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

2020-09-24 00:53小魯班one Java教程

這篇文章主要介紹了java 將字符串、list 寫入到文件,并讀取內容的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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();
        }
    }
 
    /**
     * 字節輸出到txt
     *
     * @param path
     * @param data
     */
    @SuppressWarnings("resource")
    public static void FileString2(String path, String data) {
        try {
            FileOutputStream outputStream = new FileOutputStream(path);// 字節流
            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();
        }
    }
 
    /**
     * 設置編碼格式寫出到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 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();
        }
    }
 
    /**
     * 存儲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();
        }
    }
 
    /**
     * 設置編碼格式存儲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();
        }
    }
 
    /**
     * 讀取本地文件數據設置指定編碼
     *
     * @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();
    }
 
    /**
     * 根據字節讀取文件
     *
     * @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();
    }
}

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

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

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

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

讀取代碼,這個代碼也是網上找的,忘了哪個博客了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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這個類導入的包是Io的,不是Nio的

2. ReaderFileLine.getFileContent("C:\\Users\\Lenovo\\Desktop\\hello.txt"); 這個路徑是絕對路徑

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

以上這篇java 將字符串、list 寫入到文件,并讀取內容的案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/m0_37143081/article/details/88947035

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品露脸国语对白手机视频 | 无人在线高清免费看 | 久久精品国产清白在天天线 | 久久精品99国产精品日本 | 欧美成人三级伦在线观看 | 亚洲福利一区 | 亚洲国产天堂久久精品网 | 精品推荐国产麻豆剧传媒 | 99午夜高清在线视频在观看 | 国产一区二区精品 | 草莓视频丝瓜 | 天天翘 | 亚洲免费精品视频 | 亚洲AV久久无码精品九号 | 亚洲第一se情网站 | 好逼天天有 | 2019国内精品久久久久久 | 热99re久久精品精品免费 | 国产伦精一区二区三区视频 | 九九热视频 这里有精品 | 91东航翘臀女神在线播放 | 亚洲精品乱码久久久久久蜜桃欧美 | 五月天精品在线 | 9久re热视频这里只有精品 | 久久人妻少妇嫩草AV無碼 | 国产成人小视频在线观看 | 色综合色狠狠天天综合色 | 免费在线公开视频 | 成人国产第一区在线观看 | 女生被草| 久久国产视频网站 | 欧美最猛性xxxxx动态图 | 亚洲精品色婷婷在线影院麻豆 | 免费大片a一级一级 | 国产亚洲视频网站 | 国产精品成人免费福利 | 国产麻豆91网在线看 | 日韩中文字幕网站 | 99re8在这里只有精品2 | 日韩在线 中文字幕 | 视频在线91|