zoukankan      html  css  js  c++  java
  • 读文件/写文件。http请求。读取文件列表。

    package transfor;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.ArrayList;
    public class demo {
    
        private static ArrayList<String> filelist = new ArrayList<String>();
        private static String CHARSET = "utf-8";
    
        public static void main(String[] args) throws IOException {
             refreshFileList("F:\11");
             for(int i = 0; i<filelist.size();i++) {
                 long a = System.currentTimeMillis();
                 System.out.println(filelist.get(i));
                 String text ="";
    
                 String wstr = filelist.get(i).replace(".json", "")+".txt";
    
                 text = run(filelist.get(i));
    
                 putconent(text,wstr);
    
                 long b = System.currentTimeMillis();
    
                 System.out.println("程序运行时间: "+(b-a)+"ms");
             }
             filelist.clear();
        }
    
        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());
                }
            }
        }
    
        private static void putconent(String content, String filename) {
            try {
                BufferedWriter bw = new BufferedWriter(
                        new OutputStreamWriter(
                                new FileOutputStream(filename)
                        )
                );
                bw.write(content);
                bw.flush();
                bw.close();
    
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        public static String getContent(String filename)throws Exception {
            String ss = "";
            BufferedReader br = new BufferedReader(new InputStreamReader( 
                    new FileInputStream(filename), "utf-8")); 
            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 = getContent(filename);
    
                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();
    
            } catch (Exception e) {
                rtn = "失败";
            }
            try {
    
                System.out.println("结果:"+rtn);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rtn;
        }
    }
  • 相关阅读:
    Mysql Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode
    vs2012+ winform+.net4.0发布如何在xp上运行
    ubuntu下手动配置apache2.4.12
    mysql连接错误解决(ERROR 2049 (HY000): Connection using old (pre-4.1.1) authentication protocol ref used (client option 'secure_auth' enabled))
    位运算取绝对值
    位运算两数交换
    java mysql prepareStatement模糊查询like使用注意
    idea14远程调试linux下的tomcat
    web视频播放插件:Video For Everybody
    cmd杀死进程
  • 原文地址:https://www.cnblogs.com/dhName/p/10620968.html
Copyright © 2011-2022 走看看