Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.ppcxy.cyfm.showcase.demos.utilities.io; import com.google.common.base.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.ReaderInputStream; import org.apache.commons.io.output.WriterOutputStream; import org.junit.Test; import org.springside.modules.utils.Exceptions; import java.io.*; import java.util.List; /** * ??IO?. * * @author calvin */ public class IODemo { @Test public void workWithFileContent() { File file = new File("woop.txt"); File destFile = new File("bar.txt"); try { // text -> file, Collection<String>, byte[] ->file FileUtils.writeStringToFile(file, "Hey sailor!\nHaha\n", "UTF-8"); // inputstream -> file InputStream source = IOUtils.toInputStream("Hej", "UTF-8"); FileUtils.copyInputStreamToFile(source, file); // /////////////////////////// // file -> outputstream System.out.println("copy File to outputstream:"); FileUtils.copyFile(file, System.out); // file -> file FileUtils.copyFile(file, destFile); // file -> string System.out.println("File to String:"); System.out.println(FileUtils.readFileToString(file, "UTF-8")); // file -> list<string> System.out.println("File to List<String>:"); List<String> lines = FileUtils.readLines(file, "UTF-8"); for (String string : lines) { System.out.println(string); } } catch (IOException e) { Exceptions.unchecked(e); } } /* * String/Input/OutputStream/Reader/Writer */ @Test public void workWithStream() { InputStream in = null; try { String content = "Stream testing"; // String - > InputStream. in = IOUtils.toInputStream(content, "UTF-8"); // String - > OutputStream System.out.println("String to OutputStram:"); IOUtils.write(content, System.out, "UTF-8"); // ////////////////// // InputStream/Reader -> String System.out.println("\nInputStram to String:"); System.out.println(IOUtils.toString(in, "UTF-8")); // InputStream/Reader -> OutputStream/Writer ???. InputStream in2 = IOUtils.toInputStream(content); // ?inputSteam System.out.println("InputStream to OutputStream:"); IOUtils.copy(in2, System.out); // ///////////////// // InputStream ->Reader InputStreamReader reader = new InputStreamReader(in, Charsets.UTF_8); // Reader->InputStream ReaderInputStream in3 = new ReaderInputStream(reader, Charsets.UTF_8); // OutputStream ->Writer OutputStreamWriter writer = new OutputStreamWriter(System.out, Charsets.UTF_8); // Writer->OutputStream WriterOutputStream out2 = new WriterOutputStream(writer, Charsets.UTF_8); // //////////////////// // WriterString. StringWriter sw = new StringWriter(); sw.write("I am String writer"); System.out.println("\nCollect writer content:"); System.out.println(sw.toString()); } catch (IOException e) { Exceptions.unchecked(e); } finally { // ?Stream IOUtils.closeQuietly(in); } } @Test public void workWithFileAndDir() { // FileUtilsJavaDoc?? } }