`
saluya
  • 浏览: 119265 次
  • 性别: Icon_minigender_2
  • 来自: 西安
社区版块
存档分类
最新评论

读写txt文件

阅读更多

读取txt文件:

/**
	 * 读取txt文件的整行
	 * @return
	 * @throws IOException
	 */
	public List<String> read(String fileName) throws IOException {
		List<String> txtList = new ArrayList<String>();
		BufferedReader in = new BufferedReader(new FileReader(fileName));
		String line = null;
		while ((line = in.readLine()) != null) {
			txtList.add(line);
		}
		return txtList;
	}

 写入txt文件:

/**
	 * 将"Hello world!"写入txt文件("Hello world!"可以换为字符串)
	 * @param fileName txt文件路径
	 */
	public void writeTpTxt(String fileName) {
		try {
			File file = new File(
					fileName);
			FileWriter fw = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write("Hello world!");
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics