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 ...
随机推荐
- PCM数据格式,多少字节算一帧
转自:http://blog.chinaunix.net/uid-9185047-id-3327302.html Somehow i remember that normally 2048 sampl ...
- 没有为扩展名“.html”注册的生成提供程序
asp.net提示“ 没有为扩展名“.html”注册的生成提供程序... ” 解决办法,修改web.config文件: <compilation debug="true"&g ...
- [译]:Orchard入门——导航与菜单
原文链接:Navigation and Menus 文章内容基于Orchard1.8版本.同时包含Orchard 1.5之前版本的导航参考 Orchard有许多不同的方法来创建菜单.本文将介绍两种较为 ...
- 在编译命令行中添加 /D_SCL_SECURE_NO_DEPRECATE
问题:Add the option /D_SCL_SECURE_NO_DEPRECATE to the compilation command 解决方案:项目属性 –> 配置属性 –> C ...
- Python中实现从目录中过滤出指定文件类型的文件
摘自:http://www.jb51.net/article/60641.htm #!/usr/bin/env python import glob import os os.chdir(“./”) ...
- 【原】iOS学习之UIApplication及其代理
1. 什么是UIApplication UIApplication 对象是应用程序的象征,不能手动创建,不能 alloc init,一个应用程序只允许 一个 . 每个应用都有自己的 UIApplica ...
- A*算法——启发式搜索
A*算法 本质还是搜索:加了优化而已 关于这个优化,听到两种说法: 1.剪枝 通过判断预计最少还要几步,加强版剪枝 比如说一个经典剪枝: 如果 步数≥已知最小值 则 剪枝 升级| V 如果 步数+最少 ...
- C#自定义控件属性显示在属性面板中操作
private Color controleColor; [Browsable(true)] [Description("控件颜色"), Category("自定义&qu ...
- [转]Flash Player、AIR、Flex SDK 大全
平时不断看到有朋友在各种论坛.空间.知道.群里求 Flash 平台各种版本的运行时(Flash Player)和SDK(Flex.AIR).今天就看到不下10次!所以决定把 Macromedia.Ad ...
- java异常捕获
类ExampleA继承Exception,类ExampleB继承ExampleA. 有如下代码片断: try { throw new ExampleB("b") } catch(E ...