common-io 包常用工具类

commons-io包中工具类常用 对文件的操作方法:

实例1:

InputStream in = new URL("https://a.jpg").openStream();
byte[] b = IOUtils.toByteArray(in);
FileUtils.writeByteArrayToFile(new File("test.jpg"), b);

实例2:

InputStream in = new URL( "http://www.taofen8.com" ).openStream();
//        try {
//            InputStreamReader inR = new InputStreamReader( in );
//            BufferedReader buf = new BufferedReader( inR );
//            String line;
//            while ( ( line = buf.readLine() ) != null ) {
//                System.out.println( line );
//            }
//        } finally {
//            in.close();
//        }

        try {
            System.out.println( IOUtils.toString( in ) );
        } finally {
            IOUtils.closeQuietly(in);
        }

实例3:

File file = new File(MyConstants.fileName);

List<String> lines=new ArrayList<String>();
lines.add("hello");
lines.add("world");

FileUtils.writeLines(file, lines, true);

lines = FileUtils.readLines(file, "UTF-8");

System.out.println(lines);

 

发表评论

邮箱地址不会被公开。