读文件/写文件。
Posted dhname
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读文件/写文件。相关的知识,希望对你有一定的参考价值。
package com.dingheng; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; public class wqqqqqqqqqqq { private static ArrayList<String> filelist = new ArrayList<String>(); private static String CHARSET = "utf-8"; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // int wenjian = 1; // while(wenjian<101) { // ArrayList<String> filelist = new ArrayList<String>(); // System.out.println(wenjian); // refreshFileList("E:\\1111\\333333"+Integer.toString(wenjian)+".txt"); refreshFileList("F:\\11"); for(int i = 0; i<filelist.size();i++) { long a = System.currentTimeMillis(); System.out.println(filelist.get(i)); String text =""; // String st = "E:\\3333\\333333"+Integer.toString(wenjian); // File file = new File(st); // if(!file.exists()) file.mkdirs(); // if(i==0) System.out.println(file); // String wstr = file+"\\"+Integer.toString(i+1)+".txt"; String wstr = filelist.get(i).replace(".json", "")+".txt"; // String wstr = +Integer.toString(i)+".txt"; // String st = filelist.get(i).replace(", newChar); // String wstr = st+"\\"+Integer.toString(i)+".txt"; // File file = new File(st); FileOutputStream outfile = new FileOutputStream(wstr); OutputStreamWriter osw= new OutputStreamWriter(outfile, "UTF-8"); BufferedWriter bufout = new BufferedWriter(osw); text = run(filelist.get(i)); bufout.write(text); bufout.flush(); bufout.close(); long b = System.currentTimeMillis(); System.out.println("程序运行时间: "+(b-a)+"ms"); // System.out.println(filelist.get(i)); } filelist.clear(); // wenjian++; // } } public static void refreshFileList(String strPath) { File dir = new File(strPath); File[] files = dir.listFiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { refreshFileList(files[i].getAbsolutePath()); } else { String strFileName = files[i].getAbsolutePath().toLowerCase(); filelist.add(files[i].getAbsolutePath()); } } } public static String getContent(String filename)throws Exception { String ss = ""; BufferedReader br = new BufferedReader(new InputStreamReader( //智思褒贬义xml20180123 智思褒贬义xml20180126_1 new FileInputStream(filename), "utf-8")); //智思褒贬义xml.txt String line=""; while ((line = br.readLine()) != null) { ss += line; } br.close(); return ss; } public static String run(String filename) { String rtn = ""; try { URL url = new URL("http://172.19.34.128:8801/charCorrect"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST");//POST connection.setDoInput(true); connection.setDoOutput(true); //connection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); // connection.setRequestProperty("content-type", "test/xml"); connection.setRequestProperty("content-type", "application/json"); connection.connect(); String con = ""; con = getContent(filename); long s1 = System.currentTimeMillis(); StringBuffer requestXml = new StringBuffer(); requestXml.append(con); PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), CHARSET)); try { writer.print(requestXml.toString()); writer.flush(); } catch (Exception e) { e.printStackTrace(); } finally { writer.close(); } if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("Error: " + connection.getResponseMessage()); } BufferedReader reader = null; reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), CHARSET)); StringBuffer rtnBuffer = new StringBuffer(); try { String temp = reader.readLine(); while (temp != null) { rtnBuffer.append(temp); temp = reader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { reader.close(); } rtn = rtnBuffer.toString(); long s2 = System.currentTimeMillis(); long costtime = s2 - s1; } catch (Exception e) { rtn = "-5001"; } try { System.out.println("结果:"+rtn); } catch (Exception e) { e.printStackTrace(); } return rtn; } }
以上是关于读文件/写文件。的主要内容,如果未能解决你的问题,请参考以下文章