package com.dirlist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
public class FileOutputShortcut {
/**
* 文本文件输出的快捷方式
*/
static String file="FileOutputShortcut.out";
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new StringReader(BufferedInputFile.read("src/com/dirlist/FormattedMemoryInput.java")));
PrintWriter out=new PrintWriter(file);
int lineCount=1;
String s;
while((s=in.readLine())!=null){
out.println(lineCount+++": "+s);
}
out.close();
in.close();
System.out.println(BufferedInputFile.read(file));
}
}
package com.dirlist;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class UsingRandomAccessFile {
/**
* 读写随机访问文件
*/
static String file="rtest.dat";
static void display() throws IOException{
//创建从中读取和向其中写入(可选)的随机存取文件流,该文件具有指定名称。将创建一个新的 FileDescriptor 对象来表示到文件的连接。
//mode 参数指定用以打开文件的访问模式。
RandomAccessFile rf=new RandomAccessFile(file,"r");
for(int i=0;i<7;i++){
System.out.println("value "+i+": "+rf.readDouble());
}
System.out.println(rf.readUTF());
rf.close();
}
public static void main(String[] args) throws IOException {
RandomAccessFile rf=new RandomAccessFile(file,"rw");
for(int i=0;i<7;i++){
rf.writeDouble(i*1.414);
}
rf.writeUTF("The end of file");
rf.close();
display();
rf=new RandomAccessFile(file,"rw");
rf.seek(5*8);
rf.writeDouble(45.666678);
rf.close();
display();
}
}
package com.dirlist;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StoringAndRecoveringData {
/**
* 存储和恢复数据
* @throws IOException
*/
public static void main(String[] args) throws IOException {
DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
out.writeDouble(3.1415926);
out.writeUTF("That is pi");//以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。
out.writeDouble(1.41413);
out.writeUTF("Suuare root of 2");
out.close();
DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
System.out.println(in.readDouble());
System.out.println(in.readUTF());
System.out.println(in.readDouble());
System.out.println(in.readUTF());
in.close();
}
}
package net.mindview.util;
import java.io.*;
import java.util.Arrays;
import java.util.Iterator;
import net.mindview.util.Directory.TreeInfo;
public class BinaryFile {
/**
* 读取二进制文件
*
* @throws IOException
*/
public static byte[] read(File bFile) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
bFile));
byte[] data = new byte[in.available()];
in.read(data);
in.close();
return data;
}
public static byte[] read(String bFile) throws IOException {
return read(new File(bFile).getAbsoluteFile());
}
public static int testRead(File file) throws IOException {
char[] c = new char[2];
DataInputStream in = new DataInputStream(new FileInputStream(file));
return in.readInt();//从所包含的输入流中读取此操作需要的字节。
}
public static void main(String[] args) {
try {
for(byte item:read("src/net/mindview/util/TextFile.java")){
System.out.print((char)item);
}
Iterator<File> iterator = TreeInfo.walk(".", ".*\\.class").files
.iterator();
while (iterator.hasNext()) {
File file = (File) iterator.next();
//所有的.class文件都是以CAFEBABE开头的
System.out.println(Integer.toHexString(testRead(file)).toUpperCase());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package net.mindview.util;
import java.io.*;
import java.util.*;
public class TextFile extends ArrayList<String>{
/**
* 文件读写的实用工具
* @throws IOException
*/
//Read a file as a single String
public static String read(String filename) throws IOException{
StringBuilder sb=new StringBuilder();
BufferedReader br=new BufferedReader(new FileReader(new File(filename).getAbsoluteFile()));
String s;
while((s=br.readLine())!=null){
sb.append(s+"\n");
}
br.close();
return sb.toString();
}
//Write a single file in one method call
public static void write(String filename,String text) throws IOException{
File path=new File(filename).getAbsoluteFile();
PrintWriter pw=new PrintWriter(path);
pw.print(text);
pw.close();
}
//Read a file,split by any regular expression
public TextFile(String filename,String splitter) throws IOException{
super(Arrays.asList(read(filename).split(splitter)));
//System.out.println(get(0)+"======================");
if(get(0).equals(""))
System.out.println("++++++++++++++++");
remove(0);
}
//Normally read by lines:
public TextFile(String filename) throws IOException{
this(filename,"\n");
}
public void write(String filename) throws IOException{
PrintWriter out=new PrintWriter(new File(filename).getAbsoluteFile());
for(String item:this){
out.println(item);
}
out.close();
}
public static void main(String[] args) throws IOException {
String file=read("src/net/mindview/util/TextFile.java");
write("text.txt",file);
//System.out.println(read("text.txt"));
TextFile tf=new TextFile("text.txt");
// System.out.println(read("text.txt"));
tf.write("text2.txt");
// System.out.println(read("text2.txt"));
//非词字符
TreeSet<String> words=new TreeSet<String>(new TextFile("src/net/mindview/util/TextFile.java","\\W+"));
System.out.println(words.toString());
//headSet(E toElement)返回此 set 的部分视图,要求其元素严格小于 toElement。
System.out.println(words.headSet("a"));
}
}