记得关闭流,记得关闭流,记得关闭流,
读取:
第一种:
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; import org.apache.commons.io.FileUtils; public class ReadTxt2 { public static void main(String[] args) throws IOException { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("test.txt"))); // 使用字符串换行作为分隔符 s.useDelimiter("/n"); while (s.hasNext()) { // System.out.println(s.next()); // String keyWord = URLDecoder.decode(s.next(), "utf8"); // System.out.println(keyWord); } } } }
第二种:
File file = new File("testurl.txt"); System.out.println("testurl.txt"); InputStreamReader reader = new InputStreamReader( new FileInputStream(file)); BufferedReader br = new BufferedReader(reader); String stringLine; while ((stringLine=br.readLine())!=null) { System.out.println(stringLine); }
第三种:
import java.io.BufferedReader; import java.io.FileReader; import org.junit.Test; public class TestTest { @Test public void Test1() { try { String pathname = util.Directory.GetAppPath("data") + "testurl.txt"; FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader); while (br.ready()) { System.out.println(br.readLine()); } br.close(); reader.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
写入:
//import org.apache.commons.io.FileUtils; FileUtils.write(new File("d:/textContent.html"),"写入内容", "gbk");1.