java -- IO输入与输出

1、File类

File类是IO包中唯一代表磁盘文件本身信息的类,  而不是文件中的内容 .

File类定义了一些与平台无关的方法来操纵文件,  例如: 创建, 删除文件和重命名文件 .

java中的目录被当作一种特殊的文件使用, list方法可以返回目录中的所有子目录和文件名.

在Unix下的路径分隔符为" / ", 在Dos下的路径分隔符为" ", java可以正确处理Unix和Dos的路径分隔符 .

编程举例:  判断某个文件是否存在,  存在则删除, 不存在则创建 ;   (练习一下API)

import java.io.File;
import java.util.*;
public class FileTest {
    
    public static void main(String[] args) {
        File f=new File("1.txt");
        if(f.exists())
        {
            //f.delete();
        }
        else
        {
            try{f.createNewFile();} catch(Exception e){e.printStackTrace(); }
        }
        //System.out.println("Hello Word! ");
        System.out.println("File name:"+ f.getName());
        System.out.println("File path:"+ f.getAbsolutePath());
        System.out.println("File abs path:"+ f.getAbsolutePath());
        System.out.println("File parent:"+ f.getParent());
        System.out.println(f.exists()?"exist":"not exist");
        System.out.println(f.canRead()?"read":"not read");
        System.out.println(f.isDirectory()?"directory":"not directory");
        System.out.println("File last modefied"+ new Date(f.lastModified()));
    }    
}

 2、RandomAccessFile类

3、PipedInputStream

import java.io.*; 
import java.util.*;
public class Sender extends Thread {
    private PipedOutputStream out=new PipedOutputStream();
    public PipedOutputStream getOutputStream()
    {
        return out;
    } 
    public void run()
    {
        String strInfo=new String("Hello,receiver!");
        try        
        {
            out.write(strInfo.getBytes());
            out.close();
        }
        catch(Exception e) { e.printStackTrace();}
    } 
}
import java.io.* ;
public class Receiver extends Thread {
    private PipedInputStream in=new PipedInputStream();
    public PipedInputStream getInputStream()
    {
        return in;
    } 
    public void run()
    {
        //String strInfo=new String("Hello,receiver!");
        byte[] buf=new byte [100];
        try        
        {
            int len =in.read(buf);    
            //out.write(strInfo.getBytes());
            in.close();
            System.out.println("the fllowing message comes from sender: "
                +new String(buf, 0, len));
        }
        catch(Exception e) { e.printStackTrace();}
    } 
}
import java.io.*;
public class TestPiPerStream {

    public static void main(String[] args) throws Exception {
        Sender t1=new Sender();
        Receiver t2=new Receiver();
        
        PipedOutputStream out=t1.getOutputStream();
        PipedInputStream in=t2.getInputStream();
        out.connect(in);   //管道连接 ;  
        t1.start(); 
        t2.start();
    }    
}

4、ByteArrayInputStream

//字节数组输入输出流 ; 
import java.io.* ;
public class ByteArrayTest {
    
    public static void main(String[] args) {
        String tmp="abcdefghijklmnopqrst";
        byte [] src=tmp.getBytes() ;            //只能对字节数组进行处理 ; 
        ByteArrayInputStream input=new ByteArrayInputStream(src) ;
        ByteArrayOutputStream output=new ByteArrayOutputStream() ;
        //transform(input, output);
        transform(System.in, System.out); /* 。。*/
        byte []rec= output.toByteArray();
        System.out.println(new String(rec));
    }    
    
    public static void transform(InputStream in, OutputStream out)
    {
        int ch= 0;
        try
        {
            while((ch=in.read())!= -1)   //读取输入流 结束标志为 -1 ;
            {
                int upperCh =(int)Character.toUpperCase((char)ch);
                out.write(upperCh);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}


总结(摘要):

  System.in连接到键盘, 是InputStream类型的实例对象, System.out连接到显示器, 是OutputStream类的实例对象 ;

  不管各种底层物理设备用什么方式实现数据的终止点, InputStream的read方法总数返回-1表示输入流的结束;

    在Windows下, Ctrl+Z 组合键产生键盘输入流的结束标志, 在Linux下, 则是按下Ctrl+D组合键来产生键盘输入流的结束标志 ;

 5、字符编码

大致了解了一下概念,  iso8859-1,Unicode, GB2312,  UTF-8, UTF-16 ; 

import java.io.BufferedInputStream; 
import java.io.*; 
public class CharCode {
    
    public static void main(String[] args) {
        //System.setProperty("file.encoding", "iso8859-1");
        System.getProperties().list(System.out);
        String strChina = "中国" ;
        for(int i=0; i< strChina.length(); i++)
        {
            System.out.println(Integer.toHexString((int)strChina.charAt(i)));
        }
        try
        {
            byte [] buf=strChina.getBytes("gb2312");
            for(int i=0; i<buf.length; i++)
            {
                System.out.println(Integer.toHexString(buf[i]));
            }
            
            for(int i=0; i<buf.length; i++)
            {
                //BufferedInputStream sb=new BufferedInputStream(new BufferedInputStream());
                System.out.write(buf[i]);
                //sb.flush();
            }
            System.out.write(buf,  0, 4);
        //     System.out.println();
        //    System.out.println("中国");
        }
        catch(Exception e){ e.printStackTrace();}
    }    
}
 
public class CharDecode {
    
    public static void main(String[] args)throws Exception {
        // TODO: Add your code here
        System.getProperties().put("file.encoding", "ISO8859-1");
        System.out.println("please enter a Chinese string:");
            
        byte [] buf=new byte[1024] ;
        String strInfo=null; //new String();
        int pos=0;
        int ch=0; 
        while(true)
        {
            ch= System.in.read();
            System.out.println(Integer.toHexString(ch));
            switch(ch)
            {
                case '
':
                    break; 
                case '
':
                    strInfo=new String(buf, 0, pos, "ISO8859-1"); //, "gb2312");
                    for(int i=0; i< strInfo.length(); i++)
                    {
                        System.out.println(Integer.toHexString(
                            /*(int)*/strInfo.charAt(i)));
                    }
                    System.out.println(strInfo);
                    //System.out.println(new String(strInfo.getBytes("iso8859-1"), "gb2312")); //"iso8859-1"  转化为 "gb2312" ;
                    brea;default:
                    buf[pos++]=(byte)ch;
                    break;
            }
        }
    }    
}
原文地址:https://www.cnblogs.com/ceal/p/5342757.html