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 ...
随机推荐
- Linux的tmpfs文件系统
转载:http://blog.csdn.net/wxwsixis/article/details/5752186 前几天发现服务器的内存(ram)和swap使用率非常低,于是就想这么多的资源,不用岂不 ...
- 【zz】面试题之寻找丢失的数字
据传说是MS/Google等等IT名企业的面试题: 有一组数字,从1到n,中减少了一个数,顺序也被打乱,放在一个n-1的数组里 请找出丢失的数字,最好能有程序,最好算法比较快 BTW1: 有很多种方法 ...
- 【Spring】获取资源文件+从File+从InputStream对象获取正文数据
1.获取资源文件或者获取文本文件等,可以通过Spring的Resource的方式获取 2.仅有File对象即可获取正文数据 3.仅有InputStream即可获取正文数据 package com.sx ...
- MIT 6.828 JOS学习笔记8. Exercise 1.4
Lab 1 Exercise 4 阅读关于C语言的指针部分的知识.最好的参考书自然是"The C Programming Language". 阅读5.1到5.5节.然后下载poi ...
- java导出word(带图片)
public class CreateWordDemo { public void createDocContext(String file) throws DocumentException,IOE ...
- sublime代码片段
创建方法:Tools > New Snippet 这时你会看到如下示例代码: <snippet> <content><![CDATA[ Hello, ${ ...
- 《DSP using MATLAB》示例Example5.14
代码: x1 = [1,2,2]; x2 = [1,2,3,4]; y = circonvt(x1,x2,4) n1 = 0:1:length(x1)-1; n2 = 0:1:length(x2)-1 ...
- 服务器响应HTTP的类型ContentType大全
".*"="application/octet-stream" ".001"="application/x-001" & ...
- Leetcode Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 关于history的Linux命令行
1.使用 HISTTIMEFORMAT 显示时间戳当你从命令行执行 history 命令后,通常只会显示已执行命令的序号和命令本身.如果你想要查看命令历史的时间戳,那么可以执行: # export H ...