• 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();
        }
    
    }
  • 相关阅读:
    AVL树插入操作InsertAVL的实现
    epoll中EPOLLSHOT的使用
    LeetCode79:单词搜索,以及在传参时使用引用传递的重要性
    Muduo中MutexLock类中嵌套UnassignGuard类的作用
    LeetCode84:柱状图中最大的矩形
    计算图的关节点
    迪杰斯特拉算法与佛洛依德算法
    二叉树的后序遍历,先序,中序
    最近一段的学习计划
    串:KMP算法
  • 原文地址:https://www.cnblogs.com/james-roger/p/5681247.html
Copyright © 2020-2023  润新知