package com.javatest.techzero.gui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; /**
* WriteToFile.java
*
* @author Techzero
* @Email techzero@163.com
* @Time 2013-12-11 下午3:47:11
*/
public class WriteReadFileUseBufferReaderDemo { /**
* @param args
*/
public static void main(String[] args) {
String str = null;
try {
writeToFile();
str = readFromFile();
} catch (IOException e) {
e.printStackTrace();
}
// 以空格为分隔符进行拆分
String line[] = str.split(" ");
for (String temp : line) {
System.out.println(temp);
}
} public static void writeToFile() throws IOException {
File file = new File("D:/JavaWriteReadFile.log");
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file, true);
for (int i = 0; i < 100; i++) {
StringBuffer sb = new StringBuffer();
// 写入的内容最后加上一个空格用于拆分成行
sb.append("这是第" + i + "行:Java文件写入读取测试 ");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
} @SuppressWarnings("resource")
public static String readFromFile() throws IOException {
File file = new File("D:/JavaWriteReadFile.log");
if (!file.exists() || file.isDirectory())
throw new FileNotFoundException();
BufferedReader br = new BufferedReader(new FileReader(file));
String temp = null;
StringBuffer sb = new StringBuffer();
temp = br.readLine();
while (temp != null) {
// 读取的每一行内容后面加上一个空格用于拆分成语句
sb.append(temp + " ");
temp = br.readLine();
}
return sb.toString();
}
}

Java 通过 BufferReader 实现 文件 写入读取 示例的更多相关文章

  1. Java学习-014-文本文件写入实例源代码(两种写入方式)

    此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 第一种:文本文件写入,若文件存在则删除原文件,并重新创建文件.源代码如下所示: /** * @fun ...

  2. pandas文件写入读取操作

    #encoding:utf8 import pandas as pd import numpy as np from pylab import * df=pd.read_csv("./pat ...

  3. 说说Java中的资源文件的读取

    最近在看spring的资源获取时发现JDK里存在几种不同方式的资源获取,因比较混乱特地总结起来帮助和我一样混乱的人理解.下面是我项目的类结构图,在 src/main/java 下有两个类 Resour ...

  4. Java实现http大文件流读取并批量插入数据库

    1.概述 请求远程大文本,使用流的方式进行返回.需要设置http链接的超时时间 循环插入到List中,使用mybatis-plus批量插入到mysql中 2.需求 两台服务器 大文件放到其中一台服务器 ...

  5. java下radomAccessFile文件写入读取

    package cn.stat.p2.demo; import java.io.FileNotFoundException; import java.io.IOException; import ja ...

  6. java使用ObjectInputStream从文件中读取对象

    import java.io.EOFException;import java.io.FileInputStream;import java.io.FileNotFoundException;impo ...

  7. Java Bean 获取properties文件的读取

    实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度.下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例. ...

  8. JAVA代码实现从文件写入东西后有读出来=========FileInputStream

    总结: 这个原理是,先把for循环里的数从程序读到文件里,然后把文件把内容读到程序里 package com.a.b; import java.io.FileInputStream; import j ...

  9. Java眼中的XML文件写入

    创建DOM方式生成XML文档 DOMTest package com.imooc.domtest.test; import java.io.File; import java.io.IOExcepti ...

随机推荐

  1. Linux的环境变量总结

    请见Github博客:http://wuxichen.github.io/Myblog/linux/2014/10/11/LinuxEnvironmentVariables.html

  2. vijos 1115 火星人

    说了那么多.事实上就是遍历全排列 #include<iostream> #include<cstdio> #include<algorithm> #include& ...

  3. 如何用 Swift 语言构建一个自定控件

    (via:破船之家,原文:How To Make a Custom Control in Swift)   用户界面控件是所有应用程序重要的组成部分之一.它们以图形组件的方式呈现给用户,用户可以通过它 ...

  4. ViewPager引导页效果实例源码

    首先大家先找到本地的sdk,然后找到Google提供的API,具体查找方法如下:sdk——>docs——>index.html——>develop——>training——&g ...

  5. C#_会员管理系统:开发六(数据搜索)

    增加界面中的搜索功能 会员资料管理界面(VIPManager.cs): 详细代码如下: using System; using System.Collections.Generic; using Sy ...

  6. Android:设置背景图和标题

    在MainActivity.onCreate(Bundle savedInstanceState){}设置背景图和标题. setTitle("wyl的diary");//标题,即左 ...

  7. IOS某个ViewController禁止自动旋转

    IOS屏幕自动旋转,强制横竖屏方法: - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientat ...

  8. 数据结构C语言版 有向图的十字链表存储表示和实现

    /*1wangxiaobo@163.com 数据结构C语言版 有向图的十字链表存储表示和实现 P165 编译环境:Dev-C++ 4.9.9.2 */ #include <stdio.h> ...

  9. 【.Net基础拾遗】品味OO继承

    0X1 引言 提起面向对象,每个人都有不同的见解.但提的最多的无非就是:对象.封装.继承.多态.差不多就是这些元素构成了面向对象设计开发的基本逻辑.面向对象编程,“对象”指的是什么?这里的" ...

  10. ognl--数据运转的催化剂

    原文链接:http://struts2.group.iteye.com/group/wiki/1353-ognl-catalyst-for-data-operation-in-struts2 首先让我 ...