亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-09-30 09:47:07 來源:腳本之家 閱讀:801 作者:小魯班one 欄目:開發技術

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

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隨便寫的,格式是一行一個英文單詞,一共五個。

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

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 寫入到文件,并讀取內容的案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

方山县| 阿拉善左旗| 哈尔滨市| 鸡东县| 崇信县| 道孚县| 登封市| 长子县| 西昌市| 大庆市| 柘荣县| 三穗县| 高雄市| 梅河口市| 巩义市| 古丈县| 临夏县| 博湖县| 东城区| 丹东市| 高碑店市| 花莲市| 平定县| 沙田区| 崇信县| 利川市| 南靖县| 台中县| 日照市| 浮山县| 中山市| 岢岚县| 县级市| 五指山市| 蒙城县| 西乡县| 吴江市| 陇川县| 土默特右旗| 陇西县| 迭部县|