永发信息网

java编程:从一个名为file的文件中逐行读取然后将读取的内容放进另一个文件file1中。

答案:3  悬赏:70  手机版
解决时间 2021-03-04 15:56
  • 提问者网友:我的未来我做主
  • 2021-03-03 17:53
java编程:从一个名为file的文件中逐行读取然后将读取的内容放进另一个文件file1中。
最佳答案
  • 五星知识达人网友:青灯有味
  • 2021-03-03 18:34

public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

public void writeFromBuffer(String filePath, String sb)throws IOException {
File file = new File(filePath);
FileWriter fw;
try {
fw = new FileWriter(file);
if (sb.toString() != null && !"".equals(sb.toString())) {
fw.write(sb.toString());
}
fw.close();
} catch (IOException e) {
throw new IOException("文件写入异常!请检查路径名是否正确!");
}

}

自己组织一下,读取的数据可以放在stringbuffer里然后在传给写入方法
全部回答
  • 1楼网友:纵马山川剑自提
  • 2021-03-03 20:32
MenuTreeInfoForm menuInfo = (MenuTreeInfoForm)form;
FormFile file = menuInfo.getFile();
InputStream ips = new FileInputStream( "" );
Workbook rwb = Workbook.getWorkbook(ips);
Sheet st = rwb.getSheet(0);
List infoList = new ArrayList();
List infoArrList = new ArrayList();
for(int i = 0; i < st.getRows(); i++) {

String name= st.getCell(0, i).getContents().trim();
String age = st.getCell(1, i).getContents().trim();
String sex = st.getCell(2, i).getContents().trim();
infoArrList.add(0, name);
infoArrList.add(1, age);
infoArrList.add(2, sex);
infoList.add(i,infoArrList);
}

//写如文件
OutputStream ops = new FileOutputStream("D:\test");
try {
WritableWorkbook wwb = Workbook.createWorkbook(ops);
WritableSheet ws = wwb.createSheet("new sheet", 0);
// 设定指定列的宽度
ws.setColumnView(0, 15);
ws.setColumnView(1, 15);
ws.setColumnView(2, 15);
ws.setColumnView(3, 15);
// 设定标签颜色
WritableFont wf_merge = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false,UnderlineStyle.SINGLE_ACCOUNTING, Colour.RED);
WritableCellFormat wff_merge = new WritableCellFormat(wf_merge);
wff_merge.setBackground(Colour.WHITE);
// 设置首行标题栏
Label label00 = new Label(0, 0, "操作状态", wff_merge);
ws.addCell(label00);
Label label10 = new Label(1, 0, "帐号", wff_merge);
ws.addCell(label10);
Label label20 = new Label(2, 0, "密码", wff_merge);
ws.addCell(label20);
Label label30 = new Label(3, 0, "sip", wff_merge);
ws.addCell(label30);
int infoListSize = infoList.size();
UpLoadInfo upLoadInfo = null;
if(infoListSize>0){
for (int i = 1; i <= infoListSize; i++) {
List list = infoList.get(i-1);
for(int j = 1; j <= infoArrList.size(); j++){
ws.addCell(new Label(j, j, infoArrList.get(j)));
}
}
}
wwb.write();
wwb.close();
ops.close();
}catch (Exception e) {
}
  • 2楼网友:旧脸谱
  • 2021-03-03 19:23
网上应该很多啊
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯