java生成批量txt文件
发布网友
发布时间:2022-04-23 14:46
我来回答
共2个回答
热心网友
时间:2023-10-19 07:06
Java生成批量txt文件,可以通过循环便利生成,示例如下:
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-disposition", "inline;filename=\""+ new String( fileName.getBytes("gb2312"), "ISO8859-1" )+"\";");
HashMap paras = new HashMap();
paras = (HashMap)model.get("paras");
//要导出的文件,其实是Json对象,通知我们要导出哪些表
String files = MapUtils.getString(paras, "file");
//解析成数组
String[] file = files.split(",");
//获取压缩包文件名
String fileName = SysParaConfig.getProperty("fileName");
String fileName = new String(fileName + ".zip");
File[] files = new File[file.length];
String path = request.getRealPath("test/download");
//循环遍历生成文件
for(int i = 0;i < file.length;i++){
String table = file[i];
File toFile = new File(path+"/"+table+".TXT");
if(!toFile.exists()){
toFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(toFile);
StringBuffer sbf= new StringBuffer();
//结果集,按一定规则(比如数据间隔符)查询表
String resultSql = "";
String fieldSql = "";
ListfileData =
jdbcTemplate.queryForList(fieldSql + " union all " +resultSql );
int dataSize = fileData.size();
for(int j = 0; j < dataSize; j++){
String result = (String)fileData.get(j).get("data");
sbf.append(result);
if(j != dataSize -1){
sbf.append("\r\n");
}
}
}
fos.write(strBuf.toString().getBytes("GBK"));
fos.flush();
fos.close();
}
热心网友
时间:2023-10-19 07:06
Java生成批量txt文件,可以通过循环便利生成,示例如下:
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-disposition", "inline;filename=\""+ new String( fileName.getBytes("gb2312"), "ISO8859-1" )+"\";");
HashMap paras = new HashMap();
paras = (HashMap)model.get("paras");
//要导出的文件,其实是Json对象,通知我们要导出哪些表
String files = MapUtils.getString(paras, "file");
//解析成数组
String[] file = files.split(",");
//获取压缩包文件名
String fileName = SysParaConfig.getProperty("fileName");
String fileName = new String(fileName + ".zip");
File[] files = new File[file.length];
String path = request.getRealPath("test/download");
//循环遍历生成文件
for(int i = 0;i < file.length;i++){
String table = file[i];
File toFile = new File(path+"/"+table+".TXT");
if(!toFile.exists()){
toFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(toFile);
StringBuffer sbf= new StringBuffer();
//结果集,按一定规则(比如数据间隔符)查询表
String resultSql = "";
String fieldSql = "";
ListfileData =
jdbcTemplate.queryForList(fieldSql + " union all " +resultSql );
int dataSize = fileData.size();
for(int j = 0; j < dataSize; j++){
String result = (String)fileData.get(j).get("data");
sbf.append(result);
if(j != dataSize -1){
sbf.append("\r\n");
}
}
}
fos.write(strBuf.toString().getBytes("GBK"));
fos.flush();
fos.close();
}
热心网友
时间:2023-10-19 07:06
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class File_creater {
public static void main(String s[])
{
try {
for(int i=1;i<200;i++)
{
String txtname=i+".txt";
String txtText=i/100+1+"."+i%100;
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(
////"out\\"+ 需要就自己加上文嘉嘉名称
txtname)));
out.println(txtText);
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
热心网友
时间:2023-10-19 07:06
Java生成批量txt文件,可以通过循环便利生成,示例如下:
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-disposition", "inline;filename=\""+ new String( fileName.getBytes("gb2312"), "ISO8859-1" )+"\";");
HashMap paras = new HashMap();
paras = (HashMap)model.get("paras");
//要导出的文件,其实是Json对象,通知我们要导出哪些表
String files = MapUtils.getString(paras, "file");
//解析成数组
String[] file = files.split(",");
//获取压缩包文件名
String fileName = SysParaConfig.getProperty("fileName");
String fileName = new String(fileName + ".zip");
File[] files = new File[file.length];
String path = request.getRealPath("test/download");
//循环遍历生成文件
for(int i = 0;i < file.length;i++){
String table = file[i];
File toFile = new File(path+"/"+table+".TXT");
if(!toFile.exists()){
toFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(toFile);
StringBuffer sbf= new StringBuffer();
//结果集,按一定规则(比如数据间隔符)查询表
String resultSql = "";
String fieldSql = "";
ListfileData =
jdbcTemplate.queryForList(fieldSql + " union all " +resultSql );
int dataSize = fileData.size();
for(int j = 0; j < dataSize; j++){
String result = (String)fileData.get(j).get("data");
sbf.append(result);
if(j != dataSize -1){
sbf.append("\r\n");
}
}
}
fos.write(strBuf.toString().getBytes("GBK"));
fos.flush();
fos.close();
}
热心网友
时间:2023-10-19 07:06
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class File_creater {
public static void main(String s[])
{
try {
for(int i=1;i<200;i++)
{
String txtname=i+".txt";
String txtText=i/100+1+"."+i%100;
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(
////"out\\"+ 需要就自己加上文嘉嘉名称
txtname)));
out.println(txtText);
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
热心网友
时间:2023-10-19 07:06
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class File_creater {
public static void main(String s[])
{
try {
for(int i=1;i<200;i++)
{
String txtname=i+".txt";
String txtText=i/100+1+"."+i%100;
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(
////"out\\"+ 需要就自己加上文嘉嘉名称
txtname)));
out.println(txtText);
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}