嗨我需要从文件中删除重复项并以相反的顺序打印该文件,例如“Ram is Ram is the Teacher”,然后输出应为“Teacher the is Ram”。
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
public void readAndPrintInReverseOrder() throws IOException {
String path = "test.txt";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path));
Stack<String> lines = new Stack<String>();
String line = br.readLine();
while(line != null) {
lines.push(line);
line = br.readLine();
}
while(! lines.empty()) {
System.out.println(lines.pop());
}
} finally {
if(br != null) {
try {
br.close();
} catch(IOException e) {
}
}
}
}
答案 1 :(得分:0)
package com.himanshu.factorypattern;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class DuplicateString {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String sCurrentLine;
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("D:/him.txt"));
while((sCurrentLine=br.readLine())!=null)
{ int c=0;
//System.out.println(sCurrentLine);
String[] p = sCurrentLine.split(" ");
Map<Integer,String> l = new TreeMap<Integer,String>();
for(int i=0;i<p.length;i++){
if(l.containsValue(p[i])){
}
else{
l.put(c++,p[i]);
//System.out.println(l);
}
}
Set ss = l.keySet();
Iterator it = ss.iterator();
while(it.hasNext()){
//Map.Entry m = (Map.Entry)it.next();
int k= (int)it.next();
System.out.print(l.get(k)+" ");
//System.out.println(sCurrentLine);
}
System.out.println();
}
}
catch(Exception e){
}
finally{
br.close();
}
/*String s = "I am the logic the current the current";
String[] p = s.split(" ");
Map<Integer,String> l = new TreeMap<Integer,String>();
for(int i=0;i<p.length;i++){
l.put(p[i].hashCode(),p[i]);
}
Set ss = l.keySet();
Iterator it = ss.iterator();
while(it.hasNext()){
//Map.Entry m = (Map.Entry)it.next();
int k= (int)it.next();
System.out.print(l.get(k)+" ");
}*/
}
}