zoukankan      html  css  js  c++  java
  • Java IO

    public class InputStreamTest2 {
    
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		File file = new File("d:\data.txt");
    		InputStream in = new FileInputStream(file);
    		byte[] b = new byte[1024];
    		int off = 0;
    		
    		int len = b.length;
    		while((off=in.read(b, 0, b.length))!=-1){
    			System.out.println(new String(b,0,off));
    		}
    		in.close();
    	}
    
    }
    

    拷贝指定的文件:

    public class copyFileTest {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File src = new File("d:\data.txt");
            File desc = new File("d:\desc.txt");
            
            new copyFileTest().copyFile(src, desc);
        }
        public void copyFile(File src,File desc) throws IOException{
            FileInputStream in = new FileInputStream(src);
            FileOutputStream out = new FileOutputStream(desc);
            byte b[] = new byte[1024];
            int start=0;
            while((start=in.read(b, 0, b.length))!=-1){
                out.write(b, 0, start);
                out.flush();
            }
            in.close();
            out.close();
        }
    }

     字符流:

    public class ReaderWriterTest {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            FileInputStream in = new FileInputStream("d:\reader.txt");
            FileOutputStream out = new FileOutputStream("d:\writer.txt");
            InputStreamReader isr = new InputStreamReader(in);
            OutputStreamWriter osw = new OutputStreamWriter(out);
            char b[] = new char[1024];
            int c;
            while((c=isr.read(b, 0, b.length))!=-1){
                String s = new String(b, 0, c);
                System.out.print(s);
                osw.write(b, 0, c);
                osw.flush();
            }
            isr.close();
            osw.close();
        }
    
    }
  • 相关阅读:
    CMD命令点滴
    Android之Service的使用
    JavaScript第一章
    Android之BroadcastReceiver的使用
    Android之短信验证
    HTML学习(一)
    新一代联合国秘书长?
    粗茶淡饭
    Access里的Case效果 解决“IErrorInfo.GetDescription 因 E_FAIL(0x80004005) 而失败!”问题!
    利用接口实现多种数据库类型的灵活更换
  • 原文地址:https://www.cnblogs.com/james-roger/p/5681247.html
Copyright © 2011-2022 走看看