请问java中怎样实现txt文件特定行列的读取?
答案:3 悬赏:20 手机版
解决时间 2021-01-19 16:34
- 提问者网友:沉默的哀伤
- 2021-01-19 05:02
请问java中怎样实现txt文件特定行列的读取?
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-01-19 06:38
百度这个高质量回答设计的真垃圾,别人都采纳了还不让人看到,浪费精力,真是日了狗了。
代码如下:
public static void main(String[] args) {
// TODO Auto-generated method stub
String txtPath = "C:/testReadLine.txt";
int lineNo = 2;
System.out.println(readTxtLine(txtPath,lineNo));
}
public static String readTxtLine(String txtPath, int lineNo) {
String line = "";
String encoding="GBK";
try {
File txtFile = new File(txtPath);
InputStream in = new FileInputStream(txtFile);
InputStreamReader read = new InputStreamReader(in,encoding);
BufferedReader reader = new BufferedReader(read);
int i = 0;
while (i < lineNo) {
line = reader.readLine();
i++;
}
reader.close();
} catch (Exception e) {
// TODO: handle exception
}
return line;
}
代码如下:
public static void main(String[] args) {
// TODO Auto-generated method stub
String txtPath = "C:/testReadLine.txt";
int lineNo = 2;
System.out.println(readTxtLine(txtPath,lineNo));
}
public static String readTxtLine(String txtPath, int lineNo) {
String line = "";
String encoding="GBK";
try {
File txtFile = new File(txtPath);
InputStream in = new FileInputStream(txtFile);
InputStreamReader read = new InputStreamReader(in,encoding);
BufferedReader reader = new BufferedReader(read);
int i = 0;
while (i < lineNo) {
line = reader.readLine();
i++;
}
reader.close();
} catch (Exception e) {
// TODO: handle exception
}
return line;
}
全部回答
- 1楼网友:话散在刀尖上
- 2021-01-19 08:27
读取每一行,并将每一行元素分解成需要的值,可以用空格来分割。
只做过这样的。
只做过这样的。
- 2楼网友:胯下狙击手
- 2021-01-19 07:06
可以用 FileReader 先把行读出来,因为是字符串,可以再用 String substring 函数把要的列取出来
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯