java 读写文件
1. 读文件
import java.io.*;
import java.util.*; public class test {
public void test_readfile(String filePath){
File file = new File(filePath);
FileReader fr = null;
BufferedReader reader = null;
try{
fr = new FileReader(file);
reader = new BufferedReader(fr);
String tmpString = "";
while((tmpString = reader.readLine()) != null){
System.out.println(tmpString);
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally {
try{
reader.close();
fr.close();
}
catch (IOException e){
e.printStackTrace();
}
}
} public static void main(String []args){
test case1 = new test();
String filePath = "D:\\test.txt";
case1.test_readfile(filePath);
}
}
2. 逐行写文件
import java.io.*;
import java.util.*; public class test {
public void test_writefile(String filePath){
File file = new File(filePath);
FileWriter fw = null;
BufferedWriter writer = null;
try{
fw = new FileWriter(file);
writer = new BufferedWriter(fw);
writer.write("hello");
writer.newLine(); //换行
writer.flush(); writer.write("123");
writer.newLine();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally {
try{
writer.close();
fw.close();
}
catch (IOException e){
e.printStackTrace();
}
}
} public static void main(String []args){
test case1 = new test();
String filePath = "D:\\test.txt";
case1.test_writefile(filePath);
}
}
3. 乱码问题解决
http://blog.csdn.net/greenqingqingws/article/details/7395213
https://my.oschina.net/heweipo/blog/384509#comment-list
java 读写文件的更多相关文章
- Java读写文件方法总结
Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java读写文件大全
java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...
- 【java】java 读写文件
场景:JDK8 将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...
- 转:Java读写文件各种方法及性能比较
干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...
- Java读写文件常用方法
一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...
- 161012、JAVA读写文件,如何避免中文乱码
1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...
- java 读写文件乱码问题
这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...
- java读写文件小心缓存数组
一般我们读写文件的时候都是这么写的,看着没问题哈. public static void main(String[] args) throws Exception { FileInputStr ...
- java读写文件
对于任何文件,不管有没有扩展名,都可以读写.切记,最后要.close();,否则效果出不来. 读操作: package com.wjy.read; import java.io.BufferedRea ...
随机推荐
- 在 windows 环境下安装 redislive
这是一篇在 windows 环境下安装 redislive 的教程! 项目地址:https://github.com/nkrode/RedisLive 配置文档:http://www.nkrode.c ...
- Only Link: Inheritance and the prototype chain
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_cha ...
- [R语言]R语言使用多线程对数据库进行大批量访问时出现无法连接问题
问题描述: 在R中使用多线程对数据库进行写入,在服务器端运行脚本(linux环境),总是在第6-7万个任务线程时,出现无法连接到数据库的问题.任务中断,错误信息为task 6xxxx failed,C ...
- Skype无法收发组消息
我用微软账户登录的Skype 发现无法收发组消息 - 提示发送消息不可用 卸了重装 - 提示 "无法发送消息, 请尝试获取最新的消息版本, 或者是组内成员使用旧版本无法同时视频和发送 ...
- spark 2.0 Vector toBreeze
def toBreeze( _v : Vector ) : BZV[Double] = { _v match { case x : org.apache.spark.mllib.linalg.Dens ...
- 利用React实现表头维度功能
这是我真正意义上地用react实现一些东西.这次分享的是一个很简单的小组件,效果图先放上来: 前端样式用的是一套框架.功能很简单,就是根据选择的维度,在成员里选择对应这个维度的选项. 首先初始化一些数 ...
- thinkphp2
- Sublime 常用插件简介
Sublime 常用插件简介 emmet: 前端必备神器.花个十几分钟学下Emmet语法 ,就可以极大提高web开发者HTML和CSS工作效率啦.一句话,牛逼---必备 CSS3: CSS3语法高 ...
- PAT (Basic Level) Practise 1040 有几个PAT(DP)
1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...
- Model & ModelMap & ModelAndView 比较ModelFactory简介
Model: 是一个接口,其实现类必继承ModelMap. ModelMap: 继承与LinkedHashMap,相当于自定义了一个map. ModelAndView: 里面封装了两个对象,其中vie ...