本文发表在 rolia.net 枫下论坛import Java.io.*;
class check_different {
public static void main (String args[]) {
System.out.println("Please enter the directory which your text file located in:");
StringBuffer stfDir = new StringBuffer();
While((char ch=(char)System.in.read())!='\n') { strDir.appendChar(ch); }
File dir = new File(strDir.toString());
File newFile = new File(dir, "Result.txt");
System.out.println("Please input your text file name:");
StringBuffer strFilename = new StringBuffer();
char ch = (char) System.in.read();
While ( ch != '\n' ) { strFilename.appendChar(ch); }
File readFrom = new File(dir,stfFilename.toString());
if(readFrom.isFile()&&readFrom.canRead()) {
RandomAccessFile rafFile = new RandomAccessFile(readFrom, "r");
RandomAccessFile resultFile = new RandomAccessFile(newFile, "rw");
String[] lineString = new String[]{};
StringBuffer strTemp = new StringBuffer();
while(rafFile.getFilePointer()<rafFile.length()) {
strTemp = rafFile.readLine();
if (lineString.length()<2) {
lineString[0] = strTemp; }
else {
boolean found = false;
for (int i=0; i<lineString.length(); i++) {
if (lineString[i] == strTemp.toString()) {found = true;}
else { found=false; }
}
if (!found) {lineString[i] = strTemp.toString();
resultFile.writeBytes(strTemp.toString());}
}
}
rafFile.close();
resultFile.close();
}
else
System.out.println("File cann't be read!");
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
class check_different {
public static void main (String args[]) {
System.out.println("Please enter the directory which your text file located in:");
StringBuffer stfDir = new StringBuffer();
While((char ch=(char)System.in.read())!='\n') { strDir.appendChar(ch); }
File dir = new File(strDir.toString());
File newFile = new File(dir, "Result.txt");
System.out.println("Please input your text file name:");
StringBuffer strFilename = new StringBuffer();
char ch = (char) System.in.read();
While ( ch != '\n' ) { strFilename.appendChar(ch); }
File readFrom = new File(dir,stfFilename.toString());
if(readFrom.isFile()&&readFrom.canRead()) {
RandomAccessFile rafFile = new RandomAccessFile(readFrom, "r");
RandomAccessFile resultFile = new RandomAccessFile(newFile, "rw");
String[] lineString = new String[]{};
StringBuffer strTemp = new StringBuffer();
while(rafFile.getFilePointer()<rafFile.length()) {
strTemp = rafFile.readLine();
if (lineString.length()<2) {
lineString[0] = strTemp; }
else {
boolean found = false;
for (int i=0; i<lineString.length(); i++) {
if (lineString[i] == strTemp.toString()) {found = true;}
else { found=false; }
}
if (!found) {lineString[i] = strTemp.toString();
resultFile.writeBytes(strTemp.toString());}
}
}
rafFile.close();
resultFile.close();
}
else
System.out.println("File cann't be read!");
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net