1、简单的文件读写(FileInputStream ,FileOutputStream )
public class testFile {
public static void main(String[] args) throws Exception {
/////////////////////FileInputStream/////////////////////
// 注意String(b,0,temp)的代码// 当最后一次时会少,回车(2个字节)的读写
FileInputStream fis = new FileInputStream("C:\\abc.txt");
byte[] b = new byte[20];
String re = "";
while (true) {
int temp = fis.read(b);
if (temp == -1)break;
re = re + new String(b, 0, temp);
}
fis.close();
System.out.println(re);
/////////////////////FileOutputStream/////////////////////
FileOutputStream fos = new FileOutputStream("C:\\bc.txt");
fos.write("rensongxin".getBytes());
fos.close();
}
}
2、文件逐行读写(BufferedReader )
public class TestLine {
public static void main(String[] args) throws Exception{
FileInputStream fi=new FileInputStream("c:\\abc.txt");
InputStreamReader ir =new InputStreamReader(fi);
BufferedReader br = new BufferedReader(ir);
int i=1;
while (true){
String temp= br.readLine();
if(temp==null)break;
System.out.println(i+":"+temp);
i++;
}
br.close();
}
}
3、(DataOutputStream ,DataInputStream )
public class TestData {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("c:\\test.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(12345);
dos.close();
FileInputStream fis = new FileInputStream("c:\\test.txt");
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readInt());
dis.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}// finally
}
}
4从键盘读一行
public class TestKey {
public static void main(String[] args)throws Exception{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
while(true){
String temp=br.readLine();
if(temp.equals("bye"))break;
System.out.println(temp);
}
br.close();
}//main
}
5、重要**Properties 结合FileInputStream ,FileOutputStream 的使用
public class TestProperty {
public static void main(String[] args) throws Exception{
Properties p=new Properties();
FileInputStream fis=new FileInputStream("c:\\db.properties");
p.load(fis);//
fis.close();
String name=p.getProperty("username");
//name = new String(name.getBytes("ISO8859-1"),"GBK");//不加 会显示乱码,当username=任松鑫时
System.out.println(name);
String db=p.getProperty("db");
System.out.println(db);
p.setProperty("year", "5");
FileOutputStream fos=new FileOutputStream("c:\\db.properties");
p.store(fos, "test");//
fos.close();
}
}
///////////////////////////
/**
文件内容
server=192.168.0.23
db=oracle
port=1521
username=zhang
password=superman
#server=192.168.0.20
#db=db2
#port=152
#username=db2
#password=ibm
c:\\db.properties
*/
6、PipedOutputStream ,PipedInputStream 的使用
public class SendThread extends Thread{
private PipedOutputStream pos;
public SendThread(PipedOutputStream pos){
this.pos=pos;
}
public void run(){
DataOutputStream dos=new DataOutputStream(pos);
try{
for(int i=0;i<10;i++){
dos.writeInt(i*10);
}
dos.close();
}catch(Exception e){}
}
}
public class ReThread extends Thread{
private PipedInputStream pis;
public ReThread(PipedInputStream pis){
this.pis=pis;
}
public void run(){
DataInputStream dis=new DataInputStream(pis);
try{
for(int i=0;i<10;i++){
System.out.println(dis.readInt());
}
dis.close();
}catch(Exception e){}
}
public static void main(String []args)throws Exception{
PipedInputStream pis=new PipedInputStream();
PipedOutputStream pos=new PipedOutputStream(pis);
new ReThread(pis).start();
new SendThread(pos).start();
}
}
7、File 的使用
public class TestFile {
public static void main(String[] args) throws Exception{
File f1 = new File("c:/a/b");
if(!f1.exists()){
f1.mkdirs();
System.out.println("MKDIR");
}
File.createTempFile("abc",".txt",f1);
///////////////////////////////
String[] str = f1.list();
for(int i=0;i<str.length;i++){
System.out.println(str[i]);
}
//把f1下边的所有的子文件列出来(不要目录)
//////////////////////////////
File[] f = f1.listFiles();
for(int i = 0;i<f.length;i++){
if(f[i].isFile()){
System.out.println(f[i].getName());
System.out.println(f[i].getPath());
System.out.println(f[i].getParent());
System.out.println("*******");
}
}
File ff = new File("./");
String[] aa = ff.list();
for(int i=0;i<aa.length;i++){
System.out.println(aa[i]);
}
File file = new File("../../abc.txt");
System.out.println(file.exists());
//由于windows有不同的根目录,所以相对路径无法切换盘符
}
}
8、FilenameFilter文件的过滤
public class TestFilter {
public static void main(String[] args) throws Exception {
// /////////文件的过滤////////////////////
File f1 = new File("c:/a/b");
String[] st = f1.list(new FilenameFilter() {
public boolean accept(File f, String n) {
return n.endsWith(".txt") || n.endsWith(".java");
}
});
for (int i = 0; i < st.length; i++) {
System.out.println(st[i]);
}
}
}
9、对象的序列化(ObjectOutputStream ,ObjectInputStream ,Serializable,关键字transient)
public class WritePerson {
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("c:\\person.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
Person p=new Person();
p.setAge(23);p.setName("zhang");
p.setSex("man");
oos.writeObject(p);
oos.close();
}
}
class Person implements Serializable{
private int age;
private String name;
private Object test;
private String sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getTest() {
return test;
}
public void setTest(Object test) {
this.test = test;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class ReadPerson {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("c:\\person.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
//对象序列化实现对象深克隆
Person p=(Person)ois.readObject();
ois.close();
System.out.println(p.getName()+p.getAge());
}
}
10、nio流(容量,界限,位置的测试)
public class NioTestBuffer {
public static void main(String[] args) {
CharBuffer cb=CharBuffer.allocate(8);
cb.limit(7);
System.out.println("before"+cb.capacity()+cb.position()+cb.limit());
cb.put('a');cb.put('b');cb.put('c');
System.out.println("after"+cb.capacity()+cb.position()+cb.limit());
cb.flip();
System.out.println("flip"+cb.capacity()+cb.position()+cb.limit());
System.out.println(cb.get());
System.out.println(cb.get(2));
System.out.println("get"+cb.capacity()+cb.position()+cb.limit());
cb.clear();
System.out.println("clear"+cb.capacity()+cb.position()+cb.limit());
System.out.println(cb.get(1));
}
}
11、通道的使用FileChannel
public class TestChannel {
//利用通道文件的快速拷贝
public static void main(String[] args) throws Exception{
FileInputStream fis=new FileInputStream("c:\\abc.txt");
FileOutputStream fos=new FileOutputStream("c:\\go.txt");
FileChannel fc1=fis.getChannel();
FileChannel fc2=fos.getChannel();
ByteBuffer bb=ByteBuffer.allocate(20);
while(true){
int temp=fc1.read(bb);
bb.flip();//注意理解 位置回零
if(temp==-1)break;
fc2.write(bb);
bb.clear();//注意理解 位置回零 界限回容量
}
fc1.close();
fc2.close();
fis.close();
fos.close();
}
}
12、建立直接缓冲
public class TestDirect {
public static void main(String[] args) {
// 建立直接缓冲
ByteBuffer bb = ByteBuffer.allocateDirect(16);
CharBuffer cb = bb.asCharBuffer();// CharBuffer等其他6个直接缓冲
IntBuffer ib = bb.asIntBuffer();// 都由ByteBuffer来建立
System.out.println(bb.capacity());
System.out.println(cb.capacity());
System.out.println(ib.capacity());
}
}
13、FileChannel 的transferFrom
public class TestTransfer {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("c:\\abc.txt");
FileOutputStream fos = new FileOutputStream("c:\\go.txt");
FileChannel fc1 = fis.getChannel();
FileChannel fc2 = fos.getChannel();
fc2.transferFrom(fc1, 0, fc1.size());
// fc1.transferTo(position, count, target);
// 注意 fc1 fc2 选一个就可以 但注意参数
fc1.close();
fc2.close();
fis.close();
fos.close();
}
}