读文件/写文件。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 { /** * 以字节为单位读取文件,常用 ...
随机推荐
- Day 4 测试(QwQ 表示没考好 未完待续。。。 可能要过一段时间才会更
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- 忘掉Ghost!利用Win10自带功能,玩转系统备份&恢复 -- 系统重置
之前几篇介绍的如何备份.恢复系统,在遇到问题的时候可以轻松应对. 如果系统出现问题,还可以正常启动,但是之前没有备份过系统,那该怎么办? 碰到这种问题,可以使用Win10系统的“系统重置”功能: 按照 ...
- 关于进程、线程、应用程序域(AppDomain)
什么是进程?什么是线程?什么是应用程序集?及三者之间的关系(以下做一些简单的概念性的理解) 三者共同的基础对象!!!——应用程序 1.进程是操作系统用于隔离众多正在运行的应用程序的机制,进程的缺点是降 ...
- Web of science数据下载以数据处理
目标网站分析 我们要获取的就是这几个数值 程序实现 # -*- coding: utf-8 -*- """ @Datetime: 2019/2/28 @Author: Z ...
- 当使用makemigrations时报错No changes detected
在修改了models.py后,有些用户会喜欢用python manage.py makemigrations生成对应的py代码. 但有时执行python manage.py makemigration ...
- jvisualvm安装visualgc插件
jdk1.7自带jvisualvm可以对java应用进行监控.其中有个插件visualgc可以查看jvm垃圾回收的具体信息.安装插件的步骤是打开jvisualvm,点击工具->插件,在可用插件列 ...
- Latex "Error: File ended while scanning use of \@xdblarge"
Latex 编译时出现 Error: File ended while scanning use of \@xdblarge" 是因为少了一个 }...
- [物理学与PDEs]第2章习题3 Laplace 方程的 Neumann 问题
设 $\Omega$ 为单连通区域, 在其边界 $\vGa$ 上给定向量场 ${\bf u}_B$, 则在 $\bar\Omega$ 中存在速度场 ${\bf u}$, 使其在 $\Omega$ 中成 ...
- C++ 实现简单命令行学生管理系统
C++ 实现简单命令行学生管理系统 预览: 编译环境是macOS.system("clear") 在windows下请换成 system("cls") #inc ...
- Linux之搭建远程数据库[Ubuntu:全过程]
1.mariadb在Linux中首次进入mysql (因为此时还没有创建任何用户,mysql的root并不等效于linux中的root用户) sudo apt-get install mysql-se ...