JAVA文件操作
来源:互联网 发布:易语言自动刷图源码 编辑:程序博客网 时间:2024/06/05 15:47
1.流
java流分为处理字节和处理字符两种类型。inputStream和OutputStream可以读取单个字节或字节数组,而Reader和Writer可以读取unicode字符。四种类型都实现了Closeable接口,Closeable接口(IOException)扩展了AutoCloseable接口(可以抛出所有异常)。OutputStream和Writer还实现了Flushable接口。
java流可以使用组合过滤的形式来实现多重功能。
如读文件时使用缓存 new BufferedInputStream(new FileInputStream("/path/")
从压缩文件中读取数字 ZipInputStream zip = new ZipInputStream(new FileInputStream(new FIle(""))); DataInputStream ds = new DataInputStream(zip);
通过指定编码读取文件:new InputStreamReader(new FileInputStream("1.txt"), "ISO8859_5");
自动刷新方式写文件:PrintWriter out = new PrintWriter(new FileWriter("1.txt"), true);
java.io.InputStream
abstract int read() //读入一个字节,并返回该字节。碰到结尾返回-1
int read(byte[] b) //读入一个字节数组,返回读入的字节数。碰到结尾返回-1
int read(byte[] b, int off, int len) //读入一个字节数组,从数组off位置开始,长度len
long skip(long n) //跳过n字节,返回实际跳过的字节数
int available() //返回不堵塞情况下可获取的字节数
void mark(int readlimit) //当前位置打一个标记,超过readlimit字节后,标记失效
void reset() //返回到最后一个标记,如果没有标记,则不返回
boolean markSupported() //判断流是否允许打标记 FileInputStream--false BufferedFileInputStream--true
import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;/** * 读写字节 * * @author aron * */public class Stream {public static void main(String[] args) {/** * InputStream类有一个抽象方法 abstract int read() */try {InputStream is = new FileInputStream("/Users/aron/Desktop/1.txt");System.out.println("======read() method======");int i;System.out.println("available: " + is.available());while ((i = is.read()) != -1) {System.out.println("output: " + (char) i);}is.close();System.out.println("======read(byte[] b) method======");InputStream is_byte = new FileInputStream("/Users/aron/Desktop/1.txt");byte[] buffer = new byte[6];is_byte.read(buffer);for (byte b : buffer) {System.out.println("output: " + (char) b);}is_byte.close();System.out.println("======read(byte[] b, int off, int len) method======");InputStream is_off = new FileInputStream("/Users/aron/Desktop/1.txt");byte[] buffer_off = new byte[4];is_off.read(buffer_off, 1, 2); // 放入到buffer_off中第一个位置开始,持续两个字节for (byte b : buffer_off) {System.out.println("out: " + (char) b);}is_off.close();System.out.println("======skip(long n) method======");InputStream is_skip = new FileInputStream("/Users/aron/Desktop/1.txt");is_skip.skip(2); // 跳过两个字节while ((i = is_skip.read()) != -1) {System.out.println("output: " + (char) (i));}is_skip.close();System.out.println("======mark() reset() markSupported() method======");InputStream is_mark = new BufferedInputStream(new FileInputStream("/Users/aron/Desktop/1.txt"));System.out.println("markSupported: " + is_mark.markSupported());is_mark.mark(4); // 一开始打个标记,超过4个字节后标记失效is_mark.skip(2); // 跳过两个字节is_mark.reset(); // 重置到标记位置System.out.println("ismark: " + (char) (is_mark.read()));is_mark.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
java.io.OutputStream
abstract void write(int n) //写出一个字节的数据
void write(byte[] b)
void write(byte[] b, int off, int len) //写出所有或者某个范围的字节到数组中
void close() //冲刷并关闭输出流
void flush() //冲刷输出流
private static void leanOutputStream() throws IOException {OutputStream os = new FileOutputStream("/Users/aron/Desktop/1.txt");os.write(65); // 写入字节Abyte[] b = new byte[3];b[0] = 66;b[1] = 67;b[2] = 68;os.write(b);// 写入B C Dos.write(b, 1, 1);// 写入Cos.flush();os.close();}PrintWriter
private static void leanPritWriter() throws IOException {PrintWriter writer = new PrintWriter("/Users/aron/Desktop/1.txt");StringBuffer sb = new StringBuffer();sb.append("qwerasdf");writer.println(sb); // 写对象,调用toString()writer.print("asdf"); // 写字符串System.out.println(writer.checkError());writer.close();}
文本读入
String line;while((line = reader.readLine())!=null){ //do something with line}
字符编码
private static void leanCharset() {Charset cset = Charset.forName("utf-8");System.out.println(cset.toString()); // 字符集的标准名称Map<String, Charset> charsets = Charset.availableCharsets(); // 查看可用的字符集for (String name : charsets.keySet()) {// System.out.println(name);}String str = "中文";ByteBuffer buffer = cset.encode(str); // 编码byte[] b = buffer.array();System.out.println(b[0]);ByteBuffer bf = ByteBuffer.wrap(b);CharBuffer cbuf = cset.decode(bf);// 解码System.out.println(cbuf.toString());}
- java文件操作
- java文件操作大全
- java文件操作大全
- java文件操作大全
- java文件操作大全
- Java文件操作大全-
- java文件操作大全
- java文件操作大全
- Java文件操作详解
- Java文件操作大全
- Java文件操作大全
- java 文件操作大全
- Java文件操作详解
- java 文件操作大全
- Java文件操作详解
- java 文件操作大全
- Java 文件操作
- Java 文件操作
- 天龙网游帮会玩法详解 帮会系统详细介绍
- Python中的Lambda表达式小析
- MySQL的基础操作
- 第十三题 P035
- #leetcode#Maximal Square
- JAVA文件操作
- MySQL的数据类型
- 【eclipse】快捷键整理
- 数据表操作
- [latex]通过画图来查找符号的latex命令
- 《鸟哥的Linux私房菜》读书笔记4
- 《鸟哥的Linux私房菜》读书笔记5
- Palindrome Number
- 第四天 ThinkPHP手把手快速拼接网站(四)