IO学习记录

IO的一点学习记录

1、

View Code
package io.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

public class Input {
    public static void main(String[] args) throws IOException {
        String path = Input.class.getResource(".").getFile()+"/testio.txt";
        System.out.println(readFile(path));
        
        memoryInput();
    }
    public static String readFile(String pathname) throws IOException{
        if(isNullOrEmpty(pathname)){
            pathname=Input.class.getResource(".").getFile()+"/testio.txt";
        }
        File file=new File(pathname);
        BufferedReader reader=new BufferedReader(new FileReader(file));
        StringBuilder sb=new StringBuilder();
        String s;
        while((s=reader.readLine())!=null){
            sb.append(s+" \n");
        }
        reader.close();
        return sb.toString();
        
    }
    public static boolean isNullOrEmpty(String str){
        return str==null || str.isEmpty();
    }
    public static void memoryInput() throws IOException{
        StringReader sr=new StringReader(Input.readFile(""));
        int c;
        while((c=sr.read())!=-1){
            System.out.print((char)c);
        }
    }
}
package io.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

public class Input {
    public static void main(String[] args) throws IOException {
        String path = Input.class.getResource(".").getFile()+"/testio.txt";
        System.out.println(readFile(path));
        
        memoryInput();
    }
    public static String readFile(String pathname) throws IOException{
        if(isNullOrEmpty(pathname)){
            pathname=Input.class.getResource(".").getFile()+"/testio.txt";
        }
        File file=new File(pathname);
        BufferedReader reader=new BufferedReader(new FileReader(file));
        StringBuilder sb=new StringBuilder();
        String s;
        while((s=reader.readLine())!=null){
            sb.append(s+" \n");
        }
        reader.close();
        return sb.toString();
        
    }
    public static boolean isNullOrEmpty(String str){
        return str==null || str.isEmpty();
    }
    public static void memoryInput() throws IOException{
        StringReader sr=new StringReader(Input.readFile(""));
        int c;
        while((c=sr.read())!=-1){
            System.out.print((char)c);
        }
    }
}

2、

View Code
package io.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

public class Output {
    public static void main(String[] args) throws IOException {
        basicOutFile("");
        dataOut();
    }

    public static void basicOutFile(String fileName) throws IOException {
        if (Input.isNullOrEmpty(fileName)) {
            fileName = Input.class.getResource(".").getFile()
                    + "/testwriter.txt";
        }
        File file = new File(fileName);
        if (file.exists()) {
            System.out.println(file.delete());
        }
        BufferedReader reader = new BufferedReader(new StringReader(
                Input.readFile("")));
        // 1
        
// PrintWriter out = new PrintWriter(new BufferedWriter(new
        
// FileWriter(file)));
        
// 2
        PrintWriter out = new PrintWriter(file);
        int lineCount = 1;
        String s;
        while ((s = reader.readLine()) != null) {
            out.println(lineCount++ + s);
        }
        reader.close();
        out.close();
    }

    public static void dataOut() throws IOException {
        String fileName = Input.class.getResource(".").getFile()
                + "/testdataOut.txt";
        File file = new File(fileName);
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
                new FileOutputStream(file)));
        out.write(0);
        out.writeUTF("tttttt");
        out.writeUTF("打的费");
        out.close();
        
        DataInputStream input=new DataInputStream(new BufferedInputStream(
                new FileInputStream(file)));
        System.out.println(input.readInt());
        System.out.println(input.readUTF());
        System.out.println(input.readUTF());
    }
}
View Code
package io.file;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Channel {
    private byte[] obj=new byte[0];
    public static void main(String[] args) throws IOException, InterruptedException {
        channelTest();
    }

    public static void channelTest() throws IOException, InterruptedException {
        String fileName = Input.class.getResource(".").getFile()
                + "/testio.txt";
        File file = new File(fileName);
        FileChannel cl = new FileOutputStream(file).getChannel();
        cl.write(ByteBuffer.wrap("aaaaaaa".getBytes()));
        cl.close();
        
        cl=new RandomAccessFile(file, "rw").getChannel();
        cl.position(cl.size());
        cl.write(ByteBuffer.wrap("new teswt".getBytes()));
        cl.close();
        
        cl=new FileInputStream(file).getChannel();
        //ByteBuffer bf=ByteBuffer.allocate((int) cl.size());
        ByteBuffer bf=ByteBuffer.allocate(1);    
        
        while(cl.read(bf)!=-1){
            bf.flip();
            System.out.print((char)bf.get());            
            bf.clear();
            //Thread.sleep(200);
        }
        //cl.read(bf);
        while(bf.hasRemaining()){
            System.out.print((char)bf.get());
        }
        bf.clear();
        
        
        System.out.println(System.getProperty("file.encoding"));
        cl = new FileOutputStream(file).getChannel();
        ByteBuffer newby=ByteBuffer.allocate(1024);
        //newby.asCharBuffer().put("aaaa");
        
//cl.write(newby);
        cl.write(ByteBuffer.wrap("cccccccccc".getBytes("utf-8")));
        cl.close();
        cl=new FileInputStream(file).getChannel();
        cl.read(newby);
        newby.flip();
        //bf.rewind();
        System.out.println(newby.asCharBuffer());
        
        
    }
}

 

3、
posted @ 2012-07-06 23:39  wasp  阅读(198)  评论(0编辑  收藏  举报