
正文
java代码解析本地文件 java解析java文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
怎么用Java代码解析用丨隔开的TXT文件。 TXT文件中格式如下 123|456|789|987
你可以尝试使用 split 转换成字符串数组,再遍历数组进行操作
String[] aa = "123|456|789|987654".split("|");
aa[0] 和 aa[1] 就是前两列啦
相关问答
Q1: JAVA 解析txt文件。
java读取txt文件的内容 类
?
1.packagetxt;
2.
3.importjava.io.BufferedReader;
4.importjava.io.File;
5.importjava.io.FileInputStream;
6.importjava.io.InputStreamReader;
7.
8./**
9. * 读取TXE数据
10. */
11.publicclassReadTxtUtils {
12. publicstaticvoidmain(String arg[]) {
13. try{
14. String encoding ="GBK";// 字符编码(可解决中文乱码问题 )
15. File file =newFile("c:/aa.txt");
16. if(file.isFile() file.exists()) {
17. InputStreamReader read =newInputStreamReader(
18. newFileInputStream(file), encoding);
19. BufferedReader bufferedReader =newBufferedReader(read);
20. String lineTXT =null;
21. while((lineTXT = bufferedReader.readLine()) !=null) {
22. System.out.println(lineTXT.toString().trim());
23. }
24. read.close();
25. }else{
26. System.out.println("找不到指定的文件!");
27. }
28. }catch(Exception e) {
29. System.out.println("读取文件内容操作出错");
30. e.printStackTrace();
31. }
32. }
33.}
Q2: java 怎么对选中的excel文件进行解析 求详细实例代码
import Java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ReadExcel {
public static void readExcel(File file){
try {
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
Workbook wb = null;
// poi-3.9.jar 只可以读取2007以下的版本java代码解析本地文件,后缀为java代码解析本地文件:xsl
wb = new HSSFWorkbook(inputStream);//解析xls格式
Sheet sheet = wb.getSheetAt(0);//第一个工作表 java代码解析本地文件,第二个则为1java代码解析本地文件,以此类推...
int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex = lastRowIndex; rIndex ++){
Row row = sheet.getRow(rIndex);
if(row != null){
int firstCellIndex = row.getFirstCellNum();
// int lastCellIndex = row.getLastCellNum();
//此处参数cIndex决定可以取到excel的列数。
for(int cIndex = firstCellIndex; cIndex 3; cIndex ++){
Cell cell = row.getCell(cIndex);
String value = "";
if(cell != null){
value = cell.toString();
System.out.print(value+"\t");
}
}
System.out.println();
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
File file = new File("D:/test.xls");
readExcel(file);
}
}
关于java代码解析本地文件和java解析java文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







