import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStreamReader;
import javaioOutputStreamWriter;
public class App17 {
public static void main(String[] args) throws IOException {
File srcFile = new File("d:\\testtxt");
File outFile = new File("d:\\desttxt");
InputStreamReader reader = new InputStreamReader(new FileInputStream(srcFile));
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile));
char[] buffer = new char[5];
while(readerread(buffer) != -1) {
Systemoutprintln(buffer);
writerwrite(StringvalueOf(buffer)trim() + "\r\n");
}
readerclose();
writerflush();
writerclose();
}
}
使用java poi
package webservice;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
public class ExcelController {
@SuppressWarnings("deprecation")
public void excel() throws FileNotFoundException, IOException{
String filename = "d:\\excelxls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
//按名引用excel工作表
// HSSFSheet sheet = workbookgetSheet("JSP");
//也可以用以下方式来获取excel的工作表,采用工作表的索引值
HSSFSheet sheet = workbookgetSheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int rows=sheetgetLastRowNum();
for(int icount=0;icount<rows;icount++){
row = sheetgetRow(icount);
int line=rowgetPhysicalNumberOfCells();
for(int j=0;j<line;j++){
cell1= rowgetCell(j);
Systemoutprintln(cell1+"--"+icount+"---"+j);
}
}
//打印读取值
// Systemoutprintln(cellgetStringCellValue());
//新建一输出流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 是你另存为的路径,不处理直接写入模版文件
//存盘
workbookwrite(fout);
foutflush();
//结束关闭
foutclose();
}
public HSSFCell getCell(HSSFRow row, int index) {
// 取得分发日期单元格
HSSFCell cell = rowgetCell(index);
// 如果单元格不存在
if (cell == null) {
// 创建单元格
cell = rowcreateCell(index);
}
// 返回单元格
return cell;
}
public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
ecexcel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
eprintStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
以上就是关于java怎么读取文件中内容,并每隔5个字符换行,并写入新的文本全部的内容,包括:java怎么读取文件中内容,并每隔5个字符换行,并写入新的文本、java拿到excel数据流后如何读取excel中的数据、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)