有的时候经常为真么读写文件最合理发愁,因为JAVA提过读写文件的方式太多了(C更甚至,fopen & open又有多少人傻傻分不去,更别说ReadFile了)。

这里个人绝对比较好的写法,仅供参考。

**********************************************************************************************************************

读取文件:

**********************************************************************************************************************

public static String readFile(String file, String encode) throws Exception {
FileInputStream fileInput = new FileInputStream(file);
InputStreamReader reader = (encode==null || "".equals(encode))?new InputStreamReader( fileInput ) :new InputStreamReader( fileInput, encode);
StringBuffer sBuffer = new StringBuffer();
int charCount = 0;
char[] charBuffer = new char[1024];
while((charCount = reader.read(charBuffer)) > 0) {
sBuffer.append(charBuffer, 0, charCount);
}
reader.close();
fileInput.close();
return sBuffer.toString();
}

**********************************************************************************************************************

输入文件:

**********************************************************************************************************************

public static void writeFile(String file, String encode, String content) throws Exception {
FileOutputStream fileOutput = new FileOutputStream(file);
OutputStreamWriter writer = (encode==null || "".equals(encode))?new OutputStreamWriter(fileOutput) :new OutputStreamWriter(fileOutput, encode);
writer.write(content);
writer.close();
fileOutput.close();
}

*********************************************************************************************************************

因为读写文件时间长了。写的五花八门的。重要有自己的风格在里面才行。

以上

JAVA读文件和写文件的的代码模版的更多相关文章

  1. 总结java中创建并写文件的5种方式

    在java中有很多的方法可以创建文件写文件,你是否真的认真的总结过?下面笔者就帮大家总结一下java中创建文件的五种方法. Files.newBufferedWriter(Java 8) Files. ...

  2. Python: 读文件,写文件

    读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...

  3. Python按行读取文件、写文件

    Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...

  4. asp adodb.stream读取文件和写文件

    读取文件操作: '------------------------------------------------- '函数名称:ReadTextFile '作用:利用AdoDb.Stream对象来读 ...

  5. 快学Scala 第十五课 (二进制读取文件,写文件,访问目录,序列化)

    二进制读取文件: val file = new File("F:\\scalaWorkspace\\ScalaLearning\\files\\test.txt") val in ...

  6. java读utf8 的txt文件,第一个字符为空或问号问题

    参考:https://blog.csdn.net/yangzhichao888/article/details/79529756 https://blog.csdn.net/wangzhi291/ar ...

  7. python读文件和写文件

    f=open('D:\\wangdongjie\\files\\webservice\\baidu\\3.txt','r+') f.write('中国电视台1][][23qwe12f我是一个小小的石头 ...

  8. 使用 Java 程序写文件时,记得要 flush()

    使用 Java 程序往磁盘写文件时碰到了这样的问题:文件写不全. 假如内容(StringBuffer/StringBuilder)有 100W 个字符,但是通过 Java 程序写到文件里的却不到 10 ...

  9. Python学习入门基础教程(learning Python)--5.3 Python写文件基础

    前边我们学习了一下Python下如何读取一个文件的基本操作,学会了read和readline两个函数,本节我们学习一下Python下写文件的基本操作方法. 这里仍然是举例来说明如何写文件.例子的功能是 ...

随机推荐

  1. Jenkins忘记密码的修复方法(Windows/Linux)

    在jenkins的安装目录下,找到config.xml配置文件,删除以下节点: <useSecurity>true</useSecurity> <authorizatio ...

  2. 避免"松鼠症"

    转载: http://www.cnblogs.com/freeflying/p/7725385.html ted演讲: http://www.bilibili.com/video/av294900/

  3. C++完美实现Singleton模式[转]

    Singleton模式是常用的设计模式之一,但是要实现一个真正实用的设计模式却也不是件容易的事情.1. 标准的实现class Singleton{public: static Singleton * ...

  4. 访问php程序无法解析,排查步骤

    1.安装lamp后,php程序没有被解析 (1) apachectl -M 看是否加载了libphp5.so ,apachectl -M 这个命令查看动态libphp5.so的是否由apache加载 ...

  5. README.md文档

    大标题 =================================== 大标题一般显示工程名,类似html的\<h1\> 你只要在标题下面跟上=====即可 中标题 ------- ...

  6. JStorm文档

    Jstorm的性能测试 JStorm 大概是Apache Storm 4倍, Apache Flink 1.5 倍, Twitter Heron 2 ~ 10 倍 Jstorm是一个分布式实时计算引擎 ...

  7. Android 比ListView更好用强大的RecyclerView库:RecyclerViewLibrary

    RecyclerViewLibrary A RecyclerView libirary ,has some support, like headerAdapter/TreeAdapter,and Pu ...

  8. SSO单点登录系列6:cas单点登录防止登出退出后刷新后退ticket失效报500错

    这个问题之前就发现过,最近有几个哥们一直在问我这个怎么搞,我手上在做另一个项目,cas就暂时搁浅了几周.现在我们来一起改一下你的应用(client2/3)的web.xml来解决这个2b问题,首先看下错 ...

  9. [Angular] ngClass conditional

    Using ngClass for conditional styling, here is the usage from the docs: /** * @ngModule CommonModule ...

  10. 迭代器适配器(一)back_inserter和front_inserter的实现

    本文讨论back_inserter和front_inserter的实现. 当我们调用copy函数的时候,要确保目标容器具有足够大的空间,例如: //将other的所有元素拷贝到以coll.begin( ...