
java中读一个txt文本然后把它存到Vector对象中?看描述: 10
我想用java读取一个D:\myjava\data.txt路径的一个data.txt文本,里面内容大概就是AngelenBoycatdogEggfile然后就把每一个单词...
我想用java读取一个D:\myjava\data.txt路径的一个data.txt文本,里面内容大概就是
Angelen Boy cat dog
Egg file
然后就把每一个单词存到Vector的对象Vector<String>dict=new Vector<String>(6);
我该怎么写呢?谢谢。。
不问了。。没有一个能够可以正确的! 展开
Angelen Boy cat dog
Egg file
然后就把每一个单词存到Vector的对象Vector<String>dict=new Vector<String>(6);
我该怎么写呢?谢谢。。
不问了。。没有一个能够可以正确的! 展开
4个回答
展开全部
public Vector<String> readTest(String filePath) {
Vector<String> dict = null;
try {
FileInputStream fi = new FileInputStream(filePath);//路径不要写死
BufferedReader dr = new BufferedReader(new InputStreamReader(fi));
String line = dr.readLine();//读流
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line + " ");//把txt中的单词存在一起
line = dr.readLine();
}
String words = sb.toString();
// System.out.println(words);
if (words != null && !"".equals(words)) {//为空验证
String[] word = words.split(" ");//根据“ ”把单词拆分
// System.out.println(word.length);
dict = new Vector<String>(word.length);//Vector大小由word数组长度决定
for (int i = 0; i < word.length; i++) {
dict.add(word[i]);
}
System.out.println(dict.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dict;
}
屌丝 你确定你懂java?我了个去 还不问了 错的哥会发给你?
Vector<String> dict = null;
try {
FileInputStream fi = new FileInputStream(filePath);//路径不要写死
BufferedReader dr = new BufferedReader(new InputStreamReader(fi));
String line = dr.readLine();//读流
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line + " ");//把txt中的单词存在一起
line = dr.readLine();
}
String words = sb.toString();
// System.out.println(words);
if (words != null && !"".equals(words)) {//为空验证
String[] word = words.split(" ");//根据“ ”把单词拆分
// System.out.println(word.length);
dict = new Vector<String>(word.length);//Vector大小由word数组长度决定
for (int i = 0; i < word.length; i++) {
dict.add(word[i]);
}
System.out.println(dict.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dict;
}
屌丝 你确定你懂java?我了个去 还不问了 错的哥会发给你?
展开全部
package demo;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
public class ReadFileWords {
public static void main(String[] args) {
readTest("D:\\myjava\\data.txt");
}
public static Vector<String> readTest(String filePath) {
Vector<String> dict = null;
try {
FileInputStream fi = new FileInputStream(filePath);//路径不要写死
BufferedReader dr = new BufferedReader(new InputStreamReader(fi));
String line = dr.readLine();//读流
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line + " ");//把txt中的单词存在一起
line = dr.readLine();
}
String words = sb.toString();
//System.out.println(words);
if (words != null && !"".equals(words)) {//为空验证
String[] word = words.split(" ");//根据“ ”把单词拆分
//System.out.println(word.length);
dict = new Vector<String>(word.length);//Vector大小由word数组长度决定
for (int i = 0; i < word.length; i++) {
dict.add(word[i]);
}
System.out.println(dict.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dict;
}
}
//这货得要全的,要不什么都不会
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
public class ReadFileWords {
public static void main(String[] args) {
readTest("D:\\myjava\\data.txt");
}
public static Vector<String> readTest(String filePath) {
Vector<String> dict = null;
try {
FileInputStream fi = new FileInputStream(filePath);//路径不要写死
BufferedReader dr = new BufferedReader(new InputStreamReader(fi));
String line = dr.readLine();//读流
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line + " ");//把txt中的单词存在一起
line = dr.readLine();
}
String words = sb.toString();
//System.out.println(words);
if (words != null && !"".equals(words)) {//为空验证
String[] word = words.split(" ");//根据“ ”把单词拆分
//System.out.println(word.length);
dict = new Vector<String>(word.length);//Vector大小由word数组长度决定
for (int i = 0; i < word.length; i++) {
dict.add(word[i]);
}
System.out.println(dict.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dict;
}
}
//这货得要全的,要不什么都不会
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用 Scanner
import java.util.Scanner;
import java.util.Vector;
import java.io.File;
Scanner scanner = new Scanner(new File("D:/myjava/data.txt")).useDelimiter("\\W+");
Vector<String> dict = new Vector<String>(128);
while(scanner.hasNext()) dict.add(scanner.next());
scanner.close();
import java.util.Scanner;
import java.util.Vector;
import java.io.File;
Scanner scanner = new Scanner(new File("D:/myjava/data.txt")).useDelimiter("\\W+");
Vector<String> dict = new Vector<String>(128);
while(scanner.hasNext()) dict.add(scanner.next());
scanner.close();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
BufferedReader br = new BufferedReader(new FileReader("D:\myjava\data.txt"));
String s = br.readLine();
while(s!=null){
String words[] = s.split(" ");
for(int i=0;i<words.length;i++){
dict.add(words[i]);
}
s=br.readLine();
}
String s = br.readLine();
while(s!=null){
String words[] = s.split(" ");
for(int i=0;i<words.length;i++){
dict.add(words[i]);
}
s=br.readLine();
}
追问
你所写的第一行就显示了非法转义符。。
追答
哦,改成\\吧,我没试,随手写的\\就没问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询