读文件/写文件。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;
}
}
读文件/写文件。http请求。读取文件列表。的更多相关文章
- ASP.NET最误导人的错误提示:“未预编译文件,因此不能请求该文件”
昨天在一个ASP.NET MVC项目中,一个预编译后的视图访问时总是报错: 未预编译文件,因此不能请求该文件(The file has not been pre-compiled, and canno ...
- 文件结束符和C\C++读取文件方式
http://www.cnblogs.com/cvbnm/articles/2003056.html 约定编译器为 gcc2/x86: 所以 char, unsigned char 为 8 位, in ...
- JAVA 解决 SpringBoot 本地读取文件成功,打包后读取文件失败的方法
SpringBoot 的日常开发中,我们会发现当我们使用 InputStream input = getClass.getResource(path) 读取文件或者模板时,在 ida 中运行 测试的 ...
- day08文件操作的三步骤,基础的读,基础的写,with...open语法,文件的操作模式,文件的操作编码问题,文件的复制,游标操作
复习 ''' 类型转换 1.数字类型:int() | bool() | float() 2.str与int:int('10') | int('-10') | int('0') | float('-.5 ...
- python文件处理-读、写
Python中文件处理的操作包括读.写.修改,今天我们一起来先学习下读和写操作. 一.文件的读操作 例一: #文件读操作 f = open(file="first_blog.txt" ...
- File IO(NIO.2):读、写并创建文件
简介 本页讨论读,写,创建和打开文件的细节.有各种各样的文件I / O方法可供选择.为了帮助理解API,下图以复杂性排列文件I / O方法 在图的最左侧是实用程序方法readAllBytes,read ...
- 读、写SD上的文件请按如下步骤进行
1.调用Environment的getExternalStorageState()方法判断手机上是否插入了SD卡,并且应用程序具有读写SD卡的权限.例如使用如下代码//Environment.getE ...
- 如何有效的使用C#读取文件
如何有效的使用C#读取文件 你平时是怎么读取文件的?使用流读取.是的没错,C#给我们提供了非常强大的类库(又一次吹捧了.NET一番),里面封装了几乎所有我们可以想到的和我们没有想到的类,流是读取文件 ...
- PHP —— 读取文件到二维数组
转自:PHP读取自定义ini文件到二维数组 读取文件,可以使用file_get_contents,file,parse_ini_file等,现在有一个需求,需要读取如下格式的文件: [food] ap ...
- java读取文件多种方法
1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用 ...
随机推荐
- 初识服务器和Linux
一.什么是计算机 1.介绍 一说到计算机,我们首先想到的就是电脑,没错,电脑就是计算机,但是计算机不只是电脑. 所谓的电脑就是一种计算机,而计算机其实是:接收使用者输入的指令与资料,经中央处理器的数学 ...
- 获取本地的jvm信息,进行图形化展示
package test1; import java.lang.management.CompilationMXBean; import java.lang.management.GarbageCol ...
- OpenFlow 交换机与控制器交互步骤
1. Hello 控制器与交互及互相发送 Hello 消息.Hello消息中只包含有OpenFlow Header,其中的 type 字段为 OFPT_HELLO,version 字段为发送方所支持的 ...
- elasticsearch篇之mapping
2018年05月17日 18:01:37 lyzkks 阅读数:444更多 个人分类: Elastic stack 版权声明:文章内容来自于网络和博主自身学习体会,转载请注明出处,欢迎留言大家一起 ...
- jpg、png格式的图片转换成webp后颜色失真的问题
今天简单的试用了一下 cweb.exe 将 jpg, png 格式的图片转换成 webp 格式. 我今天下载的是当前最新版:1.0.0 cwebp 3.jpg -q 85 -o 3.webp 发现图 ...
- NodeJS跨域问题
const express = require('express'), app = express(), router = express.Router(), bodyParser = require ...
- mysql加速导入数据的简单设置
mysql加速导入数据的简单设置 # 修改前查询参数值 show variables like 'foreign_key_checks'; show variables like 'unique_ch ...
- django - 总结 - ORM性能
QuerySet 1.惰性查询 2.缓存机制 3.可迭代 4.可切片 -------------------------------------------------------在一个新创建的查询集 ...
- [物理学与PDEs]第2章第1节 理想流体力学方程组 1.4 一维理想流体力学方程组
1. 一维理想流体力学方程组 $$\beex \bea \cfrac{\p\rho}{\p t}+\cfrac{\p}{\p x}(\rho u)&=0,\\ \cfrac{\p}{\p t ...
- uploadPreview上传图片前预览图片
uploadPreview.js是一款图片上传前的预览插件.谷歌.火狐.IE都可以兼容,但是不支持safari. 相关的html代码: <!DOCTYPE html PUBLIC "- ...