求高人解除小问题,关于读取TXT文件

Even__Chung 2011-10-20 04:34:21
小弟才初学Java,目前学习进展十分缓慢,还望论坛的大哥大姐们求助,我百度了好多,自己修改了好多次,都没有按照自己意愿,输出正确的,还望大哥大姐相助,跪谢!
问题描述:
D:T\123.txt 文件
123.txt 文件内容以及规则为:

1234 0987890
2345 0979087
2433 0987099
2345 7689009
2343 7898900

小弟想读取txt文件中内容,而以上文件中内容,小弟想按照以下四种种方式输出:

第一种输出格式为:
1234
2345
2433
2345
2343

第二种输出格式为:
0987890
0979087
0987099
7689009
7898900


第三种
1:1234
2:2345
3:2433
4:2345
5:2343

第四种:
1:0987890
2:0979087
3:0987099
4:7689009
5:7898900

以上第三第四种,要求是一一对应的。
...全文
267 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2011-10-21
  • 打赏
  • 举报
回复
难道你的jdk版本不支持泛型?
import java.io.*; 
import java.util.*; //import这个包了吗?
public class Test {
public static void main(String[] args) throws Throwable {
String[] group = {"一", "二", "三", "四"};
String filename = "D:\\123.txt"; //LZ自己修改这里的路径
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
String buf;
int id = 1;
//如果不支持泛型,改成
//List<List<String>> list = new ArrayList<List<String>>();
List list = new ArrayList();
for (int i=0; i<group.length; i++) {
//list.add(new ArrayList<String>());
list.add(new ArrayList());
}
while((buf=br.readLine()) != null) {
if (!buf.matches(".*?\\s+.*")) continue;
String[] value = buf.split("\\s+", 2);
((List)list.get(0)).add(value[0]);
((List)list.get(1)).add(value[1]);
((List)list.get(2)).add(String.format("%d: %s", id, value[0]));
((List)list.get(3)).add(String.format("%d: %s", id, value[1]));
id++;
}
br.close();

for (int i=0; i<list.size(); i++) {
System.out.printf("第%s种\n", group[i]);
//for (String s : list.get(i)) {
for (int j=0; j<((List)list.get(i)).size(); j++) {
System.out.println(((List)list.get(i)).get(j));
}
}
}
}

Even__Chung 2011-10-21
  • 打赏
  • 举报
回复
我的意思是,List<List<String>> list = new ArrayList<List<String>>();
这句话引不进来,一直在报错。
qybao 2011-10-20
  • 打赏
  • 举报
回复
看来LZ还需要多多磨练啊,就帮你调试9L的代码,把可执行的贴上
import java.io.*; //这就是所谓的引用包,import语句
import java.util.*;
public class Test {
public static void main(String[] args) throws Throwable {
String[] group = {"一", "二", "三", "四"};
String filename = "D:\\123.txt"; //9L这里转义符每写好,LZ自己修改这里的路径
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
String buf;
int id = 1;
List<List<String>> list = new ArrayList<List<String>>();
for (int i=0; i<group.length; i++) {
list.add(new ArrayList<String>());
}
while((buf=br.readLine()) != null) {
if (!buf.matches(".*?\\s+.*")) continue; //9L这里少了双引号
String[] value = buf.split("\\s+", 2);
list.get(0).add(value[0]);
list.get(1).add(value[1]);
list.get(2).add(String.format("%d: %s", id, value[0]));
list.get(3).add(String.format("%d: %s", id, value[1]));
id++;
}
br.close();

for (int i=0; i<list.size(); i++) {
System.out.printf("第%s种\n", group[i]);
for (String s : list.get(i)) {
System.out.println(s);
}
}
}
}

测试数据据 D:\123.txt
1234 0987890
2345 0979087
2433 0987099
2345 7689009
2343 7898900

输出结果
第一种
1234
2345
2433
2345
2343
第二种
0987890
0979087
0987099
7689009
7898900
第三种
1: 1234
2: 2345
3: 2433
4: 2345
5: 2343
第四种
1: 0987890
2: 0979087
3: 0987099
4: 7689009
5: 7898900
qybao 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 even__chung 的回复:]
9楼要用引的包,我没有啊,总是编译不过啊。
[/Quote]
引用包你都不会?在代码前面加上import相应的包

import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Throwable {
String[] group = {"一", "二", "三", "四"};
String filename = "D:T\123.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
String buf;
int id = 1;
List<List<String>> list = new ArrayList<List<String>>();
for (int i=0; i<group.length; i++) {
list.add(new ArrayList<String>());
}
while((buf=br.readLine()) != null) {
if (!buf.matches(.*?\\s+.*)) continue;
String[] value = buf.split("\\s+", 2);
list.get(0).add(value[0]);
list.get(1).add(value[1]);
list.get(2).add(String.format("%d: %s", id, value[0]));
list.get(3).add(String.format("%d: %s", id, value[1]));
id++;
}
br.close();

for (int i=0; i<list.size(); i++) {
System.out.printf("第%s种\n", group[i]);
for (String s : list.get(i)) {
System.out.println(s);
}
}
}
}
nizhicheng 2011-10-20
  • 打赏
  • 举报
回复

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.regex.Pattern;


public class Test {

/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
ArrayList <String[]>list=new ArrayList();
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号 你可以做处理
String a[]=tempString.split(" ");
list.add(a);
line++;
}
System.out.println("第一种");
for(String a[] :list){
System.out.println(a[0]);
}
System.out.println("第二种");
for(String a[] :list){
System.out.println(a[1]);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}




public static void main(String args[]){
readFileByLines("d://a.txt");
}
}




这样 你如果再不能举二反四 估计真的没办法了
Even__Chung 2011-10-20
  • 打赏
  • 举报
回复
9楼要用引的包,我没有啊,总是编译不过啊。
Even__Chung 2011-10-20
  • 打赏
  • 举报
回复
在哪 is .close() ?
Even__Chung 2011-10-20
  • 打赏
  • 举报
回复
sunxingtao

这个帅哥的好像是可以的,我试验了一下。
sunxingtao 2011-10-20
  • 打赏
  • 举报
回复
少个is .close()
sunxingtao 2011-10-20
  • 打赏
  • 举报
回复


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class test {
public static void main(String[] args) {
test.printStr(0);
test.printStr(1);
test.printStr(2);
test.printStr(3);
}
public static void printStr(int mode){

try {
BufferedReader is = new BufferedReader(new InputStreamReader(
new FileInputStream(new File("d:\\a.txt"))));
String s = " ";
int j=0;
while ((s = is.readLine()) != null) {
j++;
String[] b = s.split(" ");
for (int i = 0; i < b.length; i++) {
if(mode==0&&i % 2 == 0){
System.out.println(b[i]);
}else if(mode==1&&i % 2 != 0){
System.out.println(b[i]);
}else if(mode==2&&i % 2 == 0){
System.out.println(j+":"+b[i]);
}else if(mode==3&&i % 2 != 0){
System.out.println(j+":"+b[i]);
}
}
}
System.out.println("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

bohe_198878 2011-10-20
  • 打赏
  • 举报
回复
9楼不都给你写好了么,拿过去运行就是了 结果都出来了
nizhicheng 2011-10-20
  • 打赏
  • 举报
回复
这还不会改啊~~~1234 0987890 每次读出来 是一行~~
然后你split(" ")就好了~~就得到2个了 你要显示哪个就显示哪一个
Even__Chung 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 nizhicheng 的回复:]
以行为单位读取文件内容,一次读一整行:
line 1: 1234 0987890
line 2: 2345 0979087
line 3: 2433 0987099
line 4: 2345 7689009
line 5: 2343 7898900
这是输出效果~~~
你要的4种方式 自己稍微改下就可以了
[/Quote]

大哥,怎么改?我都改了一个下午了。
Even__Chung 2011-10-20
  • 打赏
  • 举报
回复
9楼的看不懂啊,没有编译过啊?
nizhicheng 2011-10-20
  • 打赏
  • 举报
回复
只是读取文件的内容 没有做任何输出流~~不可能删除的~~
我的a.txt 一直都在
bohe_198878 2011-10-20
  • 打赏
  • 举报
回复
9楼大神 我崇拜你啊
Even__Chung 2011-10-20
  • 打赏
  • 举报
回复
我按照你们去做了,但是原有的文件的内容,怎么被删除了?
qybao 2011-10-20
  • 打赏
  • 举报
回复
for example
public class Test {
public static void main(String[] args) throws Throwable {
String[] group = {"一", "二", "三", "四"};
String filename = "D:T\123.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
String buf;
int id = 1;
List<List<String>> list = new ArrayList<List<String>>();
for (int i=0; i<group.length; i++) {
list.add(new ArrayList<String>());
}
while((buf=br.readLine()) != null) {
if (!buf.matches(.*?\\s+.*)) continue;
String[] value = buf.split("\\s+", 2);
list.get(0).add(value[0]);
list.get(1).add(value[1]);
list.get(2).add(String.format("%d: %s", id, value[0]));
list.get(3).add(String.format("%d: %s", id, value[1]));
id++;
}
br.close();

for (int i=0; i<list.size(); i++) {
System.out.printf("第%s种\n", group[i]);
for (String s : list.get(i)) {
System.out.println(s);
}
}
}
}
nizhicheng 2011-10-20
  • 打赏
  • 举报
回复
以行为单位读取文件内容,一次读一整行:
line 1: 1234 0987890
line 2: 2345 0979087
line 3: 2433 0987099
line 4: 2345 7689009
line 5: 2343 7898900
这是输出效果~~~
你要的4种方式 自己稍微改下就可以了
nizhicheng 2011-10-20
  • 打赏
  • 举报
回复
我已经编译通过了



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.regex.Pattern;


public class Test {

/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
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 static void main(String args[]){
readFileByLines("d://a.txt");
}
}



加载更多回复(6)

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧