如何在java中用文本文件导入数据
并输出在output.txt文本文件中我想用javaClassName<input.txt(输入重定向)导入数据该如何操作?谢啦...
并输出在output.txt文本文件中
我想用 java ClassName<input.txt (输入重定向)导入数据该如何操作?谢啦 展开
我想用 java ClassName<input.txt (输入重定向)导入数据该如何操作?谢啦 展开
展开全部
//导出
public void budgetlistExp(HttpServletRequest request,HttpServletResponse response) throws BiffException, IOException, RowsExceededException, WriteException{
List<Budgetapply> lists = (List<Budgetapply>)request.getSession().getAttribute("budgetlist");//得到session中保存的集合
System.out.println(lists.size());//打印一下数量
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");//格式化时间
String temp = request.getRealPath("")+"\\files\\储备库.xls";//得到模板地址
String outPath = request.getRealPath("")+"\\files\\outfile.xls";//缓存地址
File file = new File(outPath);//创建文件
if(file.exists()){//如果文件存在删除
file.delete();
}
FileOutputStream out = new FileOutputStream(outPath);//输出流
Workbook wb = Workbook.getWorkbook(new File(temp));//创建工作簿(temp新文件)
WritableWorkbook workbook = Workbook.createWorkbook(out,wb);
WritableSheet sheet = workbook.getSheet(0);//得到当前第一个sheet
//循环操作list集合
for(int i=0;i<lists.size();i++){
Budgetapply budgetapply = lists.get(i);//得到该对象
Label label = new Label(0,i+1,budgetapply.getItem().getItemname());//项目名称第0列,第i+1行,内容,
sheet.addCell(label);
label = new Label(1,i+1,budgetapply.getExplain());//列支说明
sheet.addCell(label);
label = new Label(2,i+1,budgetapply.getApplyamount()+"");//申报金额
sheet.addCell(label);
label = new Label(3,i+1,budgetapply.getActualamount()+"");//下达金额
sheet.addCell(label);
label = new Label(4,i+1,budgetapply.getSubject().getSubjectcode()+"");//第4列,第i+1行,内容,科目号
sheet.addCell(label);//添加到单元格
label = new Label(5,i+1,budgetapply.getSubject().getSubjectname());//科目名称
sheet.addCell(label);
label = new Label(6,i+1,budgetapply.getDepartment().getDepartmentname());//申报部门
sheet.addCell(label);
label = new Label(7,i+1,budgetapply.getSetupdate()+"");//申报时间
sheet.addCell(label);
label = new Label(8,i+1,budgetapply.getReviewdate()+"");//审批时间
sheet.addCell(label);
label = new Label(9,i+1,budgetapply.getUser().getUsername());//操作人
sheet.addCell(label);
}
workbook.write();//写入temp
workbook.close();//关闭工作薄
out.close();//关闭流
String outname="储备库.xls";//工作薄取名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;" + " filename="+new String(outname.getBytes(), "ISO-8859-1"));
File file1 = new File(outPath);//新建一个文件
FileInputStream inputStream = new FileInputStream(file1);//输出流输出
OutputStream os=response.getOutputStream();
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
os.write(buf, 0, length);
}
inputStream.close();
os.flush();
}
//导入
/**
* 文件上传
* @param request
*/
public String upload(HttpServletRequest request) {
String time = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());//获取当前时间
String uploadPath = request.getRealPath("")+"\\files"; // 上传文件的目录
String tempPath = request.getRealPath("")+"\\files\\temp"; // 临时文件目录
String filePath="";
File tempPathFile;
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(41943040); // 设置最大文件尺寸,这里是40MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
filePath=time+LzgUtil.getSuffixName(fullFile.getName());//重组的文件名 时间+后缀名 防止重复
File savedFile = new File(uploadPath,filePath);
fi.write(savedFile);
}
}
} catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
return uploadPath+"\\"+filePath;
}
这里用的jar包是jxl
public void budgetlistExp(HttpServletRequest request,HttpServletResponse response) throws BiffException, IOException, RowsExceededException, WriteException{
List<Budgetapply> lists = (List<Budgetapply>)request.getSession().getAttribute("budgetlist");//得到session中保存的集合
System.out.println(lists.size());//打印一下数量
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");//格式化时间
String temp = request.getRealPath("")+"\\files\\储备库.xls";//得到模板地址
String outPath = request.getRealPath("")+"\\files\\outfile.xls";//缓存地址
File file = new File(outPath);//创建文件
if(file.exists()){//如果文件存在删除
file.delete();
}
FileOutputStream out = new FileOutputStream(outPath);//输出流
Workbook wb = Workbook.getWorkbook(new File(temp));//创建工作簿(temp新文件)
WritableWorkbook workbook = Workbook.createWorkbook(out,wb);
WritableSheet sheet = workbook.getSheet(0);//得到当前第一个sheet
//循环操作list集合
for(int i=0;i<lists.size();i++){
Budgetapply budgetapply = lists.get(i);//得到该对象
Label label = new Label(0,i+1,budgetapply.getItem().getItemname());//项目名称第0列,第i+1行,内容,
sheet.addCell(label);
label = new Label(1,i+1,budgetapply.getExplain());//列支说明
sheet.addCell(label);
label = new Label(2,i+1,budgetapply.getApplyamount()+"");//申报金额
sheet.addCell(label);
label = new Label(3,i+1,budgetapply.getActualamount()+"");//下达金额
sheet.addCell(label);
label = new Label(4,i+1,budgetapply.getSubject().getSubjectcode()+"");//第4列,第i+1行,内容,科目号
sheet.addCell(label);//添加到单元格
label = new Label(5,i+1,budgetapply.getSubject().getSubjectname());//科目名称
sheet.addCell(label);
label = new Label(6,i+1,budgetapply.getDepartment().getDepartmentname());//申报部门
sheet.addCell(label);
label = new Label(7,i+1,budgetapply.getSetupdate()+"");//申报时间
sheet.addCell(label);
label = new Label(8,i+1,budgetapply.getReviewdate()+"");//审批时间
sheet.addCell(label);
label = new Label(9,i+1,budgetapply.getUser().getUsername());//操作人
sheet.addCell(label);
}
workbook.write();//写入temp
workbook.close();//关闭工作薄
out.close();//关闭流
String outname="储备库.xls";//工作薄取名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;" + " filename="+new String(outname.getBytes(), "ISO-8859-1"));
File file1 = new File(outPath);//新建一个文件
FileInputStream inputStream = new FileInputStream(file1);//输出流输出
OutputStream os=response.getOutputStream();
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
os.write(buf, 0, length);
}
inputStream.close();
os.flush();
}
//导入
/**
* 文件上传
* @param request
*/
public String upload(HttpServletRequest request) {
String time = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());//获取当前时间
String uploadPath = request.getRealPath("")+"\\files"; // 上传文件的目录
String tempPath = request.getRealPath("")+"\\files\\temp"; // 临时文件目录
String filePath="";
File tempPathFile;
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(41943040); // 设置最大文件尺寸,这里是40MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
filePath=time+LzgUtil.getSuffixName(fullFile.getName());//重组的文件名 时间+后缀名 防止重复
File savedFile = new File(uploadPath,filePath);
fi.write(savedFile);
}
}
} catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
return uploadPath+"\\"+filePath;
}
这里用的jar包是jxl
展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class filmchange {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File flsou=new File("E:/input.txt");
File fldec=new File("E:/output.txt");
String context="";
try{
FileReader fr=new FileReader(flsou);
FileWriter fw=new FileWriter(fldec);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw=new BufferedWriter(fw);
while(br.ready()){
context=br.readLine();
bw.write(context);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
fw.close();
fr.close();
}catch(Exception ex){
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class filmchange {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File flsou=new File("E:/input.txt");
File fldec=new File("E:/output.txt");
String context="";
try{
FileReader fr=new FileReader(flsou);
FileWriter fw=new FileWriter(fldec);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw=new BufferedWriter(fw);
while(br.ready()){
context=br.readLine();
bw.write(context);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
fw.close();
fr.close();
}catch(Exception ex){
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是指txt格式吗?
使用文件输入流读取文件吧,然后一行行获取。
如果是有固定格式的,按照你约定的格式进行解析。
使用文件输入流读取文件吧,然后一行行获取。
如果是有固定格式的,按照你约定的格式进行解析。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//转换流OutputStreamWrite
FileOutputStream fos2 = new FileOutputStream("d:/zhuanhuan.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos2);
osw.write("Microsoft");
osw.flush();
//使用指定编码的转换流OutputStreamWrite和在FileOutputStream参数加个true,
//表示不覆盖前面的,在后面添加
FileOutputStream fos4 = new FileOutputStream("d:/zhuanhuan2.txt",true);
OutputStreamWriter osw3 = new OutputStreamWriter(fos4,"ISO8859_1");
osw3.write("Apple");
osw3.flush();
FileOutputStream fos2 = new FileOutputStream("d:/zhuanhuan.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos2);
osw.write("Microsoft");
osw.flush();
//使用指定编码的转换流OutputStreamWrite和在FileOutputStream参数加个true,
//表示不覆盖前面的,在后面添加
FileOutputStream fos4 = new FileOutputStream("d:/zhuanhuan2.txt",true);
OutputStreamWriter osw3 = new OutputStreamWriter(fos4,"ISO8859_1");
osw3.write("Apple");
osw3.flush();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询