java 读取txt去掉重复和空行

Candylibin 2012-03-25 04:22:54
假如现在我E盘上有个txt文件通过流把他读出来然后去掉所有相应文字的重复和空行。

假如文件是这样的:你好

再见
你好

大概就是这个意思,去掉重复和空行。感谢各位大侠


...全文
1767 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2012-03-26
  • 打赏
  • 举报
回复
那就这样吧。用list,这样也算是现成的吗?

public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("D:\\workspace\\test\\src\\test.txt"));
List<String> lines = new ArrayList<String>();
while(true){
String line = reader.readLine();
if(line == null){
break;
}else if(line.matches("\\s*")){
continue;
}else{
if(lines.indexOf(line) == -1){
lines.add(line);
}
}
}
for(int i = 0; i < lines.size(); i++){
System.out.println(lines.get(i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Candylibin 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 abstruct 的回复:]

仅供参考!
Java code

public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("D:\\workspace\\test\\src\\t……
[/Quote]
不能用现成的函数,要自己写比较算法去掉重复,额。。。
Candylibin 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 a199231 的回复:]

亚麻的。。。。我无语

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.……
[/Quote]
不能用set了,必须用纯算法手工的
安特矮油 2012-03-26
  • 打赏
  • 举报
回复
仅供参考!

public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("D:\\workspace\\test\\src\\test.txt"));
Set set = new TreeSet();
while(true){
String line = reader.readLine();
if(line == null){
break;
}else if(line.matches("\\s*")){
continue;
}else{
set.add(line);
}
}

Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
yibo2010 2012-03-26
  • 打赏
  • 举报
回复
.trim();
去掉空格
WuShiG 2012-03-26
  • 打赏
  • 举报
回复
请问text如何导入
lliiqiang 2012-03-26
  • 打赏
  • 举报
回复
String str(String source){
StringBuilder result=new StringBuilder();
boolean number=true;
for(char the:source.toCharArray()){
if(the!='\n'){
number=true;
result.appaned(the);
}else if(number){
result.appaned(the);
number=false;
}
}
}
宏Lee 2012-03-25
  • 打赏
  • 举报
回复
亚麻的。。。。我无语

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;


public class Test {


public static void main(String[] args) {
Set values = new LinkedHashSet();
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/d.txt")));//文件的位置
String value = "";
while((value =bf.readLine())!=null){
value = value.trim();
if(!value.equals("")){
values.add(value);
}
}
Iterator it = values.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
我同意13楼的,代码是越敲才越会,多练练,这样才能提高
Candylibin 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 a199231 的回复:]

朋友,你们老师有教过
Iterator it = values.iterator();
while(it.hasNext()){
System.out.println(it.next());
}这样遍历set集合吧
[/Quote]
我是初学大哥你还是给我完整的代码我看看吧,基础差点
dryZeng 2012-03-25
  • 打赏
  • 举报
回复
楼主,自己做才有提高啊,做出来了才有成就感嘛。大家提供一个思路就可以了。

这本来就很简单的。

定义一个空map
for(){
trim去掉空格
与空字符串判断,如果是就continue,否则加入map中。
//为啥用map,因为它自动判断重复。
}
现在map里就是你想得到的。
宏Lee 2012-03-25
  • 打赏
  • 举报
回复
朋友,你们老师有教过
Iterator it = values.iterator();
while(it.hasNext()){
System.out.println(it.next());
}这样遍历set集合吧
Candylibin 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 a199231 的回复:]

我开始做的麻烦咯,这样才最好
Set values = new LinkedHashSet();
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/d.txt")));
String value = "";
while((value =bf.readLin……
[/Quote]

大侠这个for (Object string : values) 是1.5的,我是1.4
宏Lee 2012-03-25
  • 打赏
  • 举报
回复
我开始做的麻烦咯,这样才最好
Set values = new LinkedHashSet();
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/d.txt")));
String value = "";
while((value =bf.readLine())!=null){
value = value.trim();
if(!value.equals("")){
values.add(value);
}
}
for (Object string : values) {
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
宏Lee 2012-03-25
  • 打赏
  • 举报
回复
朋友,没泛型就去掉就是了嘛

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class Test {


public static void main(String[] args) {
List values = new ArrayList();

Map map = new HashMap();

try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/d.txt")));
String value = "";
while((value =bf.readLine())!=null){
value = value.trim();
if(!value.equals("")){
if(map.get(value)==null){
values.add(value);
map.put(value, true);
}
}
}
for (Object string : values) {
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
没有问题
Candylibin 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 a199231 的回复:]

一点失误,用set顺序会变,改了下
public static void main(String[] args) {
List<String> values = new ArrayList<String>();

Map<String, Boolean> map = new HashMap<String, Boolean>();

try {
BufferedReader bf =……
[/Quote]
大哥我用的1.4.2的,没有泛型
宏Lee 2012-03-25
  • 打赏
  • 举报
回复
一点失误,用set顺序会变,改了下
public static void main(String[] args) {
List<String> values = new ArrayList<String>();

Map<String, Boolean> map = new HashMap<String, Boolean>();

try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/d.txt")));
String value = "";
while((value =bf.readLine())!=null){
value = value.trim();
if(!value.equals("")){
if(map.get(value)==null){
values.add(value);
map.put(value, true);
}
}
}
for (String string : values) {
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
GIS的粉刷匠 2012-03-25
  • 打赏
  • 举报
回复

FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
String ss="";
String ssd="";
try {
fis=new FileInputStream("E:/a.txt");
br=new BufferedReader(new InputStreamReader(fis));
String str=null;
Set set=new LinkedHashSet();
try {
while((str=br.readLine())!=null)
{
char ch[]=str.toCharArray();
if(ch[0]!=ch[ch.length-1])
{
for(int i=0;i<ch.length;i++)
{
ss=ch[i]+"";
set.add(ss);
}

}

}
Iterator it =set.iterator();
while(it.hasNext())
{
ssd+=it.next();
}
System.out.print(ssd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
宏Lee 2012-03-25
  • 打赏
  • 举报
回复
这样应该能满足你要求吧
public static void main(String[] args) {
Set<String> values = new HashSet<String>();

try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:/d.txt")));
String value = "";
while((value =bf.readLine())!=null){
value = value.trim();
if(!value.equals("")){
values.add(value);
}
}
for (String string : values) {
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
Candylibin 2012-03-25
  • 打赏
  • 举报
回复
我也不知道这个文件是多大啊,咋定义,大小:9.45 KB (9,679 字节) 占用空间:12.0 KB (12,288 字节)
凉粉zzz 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用楼主 candylibin 的回复:]
假如现在我E盘上有个txt文件通过流把他读出来然后去掉所有相应文字的重复和空行。

假如文件是这样的:你好

再见
你好

大概就是这个意思,去掉重复和空行。感谢各位大侠
[/Quote]
额 我也学到这。
目前解决方式是:
char[]ch=new char[1024]; ---
Reader reader=new FileReader("f:/b.txt");
int length=reader.read(ch);
length=reader.read(ch,0,reader.read(ch));
System.out.println("长度:"+length);
这个长度就是文件字符的长度

如果要去掉空格。也可以直接用
if(xxx!=' '){

}
这样也行
加载更多回复(2)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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