怎么把TXT的内容读出 放进集合中 test.txt111 aaa222 bbb333 ccc怎么把TXT的内容读出 放进集合中 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.HashMap;import java.util.Map;public class test1 { 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; Map map=new HashMap(); int line = 1; //一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null){ //显示行号 String a[]=tempString.split(" "); map.put(a[0], a[1]); System.out.println("line " + line + ": " + tempString); System.out.println(map); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null){ try { reader.close(); } catch (IOException e1) { } } } } public static void main(String[] args){ new test1(); test1.readFileByLines("C:/Documents and Settings/ly/桌面/text.txt"); }} import java.util.*;import java.io.*;public class FileToMap { static File file = new File("C:/value.txt"); static HashMap<String, String> map = new HashMap<String, String>(); public static void toMap(File file){ try{ BufferedReader bf = new BufferedReader(new FileReader(file)); String text; while((text=bf.readLine()) != null){ String[] str = text.split("\\s+"); System.out.print(str[0] +"=="+ str[1]); System.out.println(); map.put(str[0], str[1]); } } catch(IOException e){ e.printStackTrace(); } } public static void main(String args[]){ toMap(file); }}1111==aaaa2222==bbbb22==sss333==eeee原文件:1111 aaaa2222 bbbb22 sss333 eeee public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("E:\\workspace\\test\\src\\com\\trisun\\mail\\test.txt"); FileReader fd = null; try { fd = new FileReader(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } StringBuffer sb =new StringBuffer(); BufferedReader br = new BufferedReader(fd); try { while(br.ready()){ sb.append(br.readLine()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String [] strArray = sb.toString().split(" "); List <String > list = new ArrayList<String>(); for(int i=0;i<strArray.length;i++){ list.add(strArray[i]); } for(String strTemp :list){ System.out.println(strTemp); } } 结果是:111aaa222bbb333ccc 高手帮我看看这段算法怎么写? 这句代码怎么理解 File f = new File("../");(java代码) oracle as连接数不释放 为什么调用FTPClient.retrieveFile会死锁呢 绝世问题 help me JFrame问题:如何在窗体一打开就处于最大化状态 关于异或 关于Jbuilder使用的一些问题,(新手请帮助) 请问怎么给分呢? 图形界面工具问题 RCP 获取路径~
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class test1 {
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;
Map map=new HashMap();
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null){
//显示行号
String a[]=tempString.split(" ");
map.put(a[0], a[1]);
System.out.println("line " + line + ": " + tempString);
System.out.println(map);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args){
new test1();
test1.readFileByLines("C:/Documents and Settings/ly/桌面/text.txt");
}
}
import java.io.*;public class FileToMap {
static File file = new File("C:/value.txt");
static HashMap<String, String> map = new HashMap<String, String>();
public static void toMap(File file){
try{
BufferedReader bf = new BufferedReader(new FileReader(file));
String text;
while((text=bf.readLine()) != null){
String[] str = text.split("\\s+");
System.out.print(str[0] +"=="+ str[1]);
System.out.println();
map.put(str[0], str[1]);
}
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String args[]){
toMap(file);
}
}
1111==aaaa
2222==bbbb
22==sss
333==eeee
原文件:
1111 aaaa
2222 bbbb
22 sss
333 eeee
// TODO Auto-generated method stub
File file = new File("E:\\workspace\\test\\src\\com\\trisun\\mail\\test.txt");
FileReader fd = null;
try {
fd = new FileReader(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer sb =new StringBuffer();
BufferedReader br = new BufferedReader(fd);
try {
while(br.ready()){
sb.append(br.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String [] strArray = sb.toString().split(" ");
List <String > list = new ArrayList<String>();
for(int i=0;i<strArray.length;i++){
list.add(strArray[i]);
}
for(String strTemp :list){
System.out.println(strTemp);
}
}
111
aaa
222
bbb
333
ccc