How to add a string to a big file in csharp !
I am not familiar with the stream,all help is welcome.thanx!
The fallow is the java code and i want to write it in csharp !
import java.io.File;
import java.io.*
import java.util.Locale;
import java.util.Comparator;
import java.text.Collator;
public class RandomAccessWordList implements WordList {
/** The name of the random access word list. This name
* is usually unique within a JVM.
*/
protected String name = null;
/** The file this word list maps to.
*/
protected File file = null;
/** The random access file object. This object is released
* when there are no queries.
*/
protected RandomAccessFile raf = null;
/** The comparator.
*/
protected Comparator comparator = DEFAULT_COMPARATOR;
/** Reference count. It is incremented in notifyCheckStart()
* method and decremented in notifyCheckEnd() method. So
* we can tell the current number of queries on this word list.
*/
protected int inUse = 0;
/** A lock used to synchonized access to the file.
*/
private Object lock = new Object();
/** Protected default construtor.
*/
protected RandomAccessWordList() {
}
/** Constructs a RandomAccessWordList from a File.
* The name of this word list is defaultly the file name
* (file.getName()).
*
* @param file the file to construct this word list
* @throws WordListException if file is null,
* or the file does not actually exists
*/
public RandomAccessWordList(File file)
throws WordListException {
if (file == null) {
throw new WordListException(
"file should not be null");
}
if (!file.exists()) {
throw new WordListException(
file.getName() + " not exist.");
}
this.file = file;
this.name = file.getName();
}
/** Constructs a RandomAccessWordList from a File and
* its name.
*
* @param file the file to construct this word list
* @param name the name of this word list
* @throws WordListException if file is null,
* or the file does not actually exists,
* or name is null or empty
*/
public RandomAccessWordList(File file, String name)
throws WordListException {
if (file == null) {
throw new WordListException(
"file should not be null");
}
if (!file.exists()) {
throw new WordListException(
file.getName() + " not exist.");
}
if (name == null || name.length() == 0) {
throw new WordListException(
"name should not be null or empty.");
}
this.file = file;
this.name = name;
}
/** TCSpellChecker calls this to notify that a query
* finishes. RandomAccessWordList would close the
* RandomAccessFile if it is there is no other queries
* on this word list.
*/
public synchronized void notifyCheckEnd() {
--inUse;
if (inUse == 0) {
try {
raf.close();
raf = null;
} catch (IOException ioe) {
;
}
}
}
/** TCSpellChecker calls this to notify that a query
* is about to take place. RandomAccessWordList would
* open a RandomAccessFile if it is not opened.
*/
public synchronized void notifyCheckStart() {
if (inUse == 0) {
try {
raf = new RandomAccessFile(file, "r");
} catch (FileNotFoundException fnfe) {
;
// should not reach here
// we've checked in constructor
}
}
++inUse;
}
/** Add a word to this word list. For the fact that
* the words in the word list should be ordered to
* allow a binary search, the insertion of a word is
* quite an expensive operation. A temporary file is
* opened to help move the words. The file should be
* writable for this method to succeed.
*
* @param word the word to be added to this word list
* @throws WordNotAddedException if the word already
* exists in this word list, or the file is
* read-only, or other exception occurs
*/
public synchronized void addWord(String word)
throws WordNotAddedException {
if (word == null) {
throw new WordNotAddedException();
}
// first check whether the word exists
// if does, just throw a WordNotAddedException
notifyCheckStart();
try {
if (findWord(word)) {
notifyCheckEnd();
throw new WordNotAddedException(
"Word already exists.");
}
} catch (WordNotAddedException wnae) {
throw wnae;
} catch (WordListException wle) {
notifyCheckEnd();
throw new WordNotAddedException(wle.getMessage());
}
notifyCheckEnd();
// if not, create a temp file and get prepared for the move
File tempFile;
try {
tempFile = File.createTempFile("WordList", "tmp");
} catch (IOException ioe) {
throw new WordNotAddedException(
"IOException: Can't create temp file");
}
// acquire the lock
synchronized (lock) {
try {
// move all the words into the temp file
PrintWriter pw = new PrintWriter(
new BufferedWriter(
new FileWriter(tempFile)));
BufferedReader br = new BufferedReader(
new FileReader(file));
while (br.ready()) {
pw.println(br.readLine());
}
pw.close();
br.close();
// move back
pw = new PrintWriter(
new BufferedWriter(
new FileWriter(file)));
br = new BufferedReader(
new FileReader(tempFile));
boolean added = false;
while (br.ready()) {
String currentWord = br.readLine();
// this is the place where the word should be inserted
if (!added && comparator.compare(word, currentWord) < 0) {
pw.println(word);
added = true;
}
pw.println(currentWord);
}
// oops, it becomes the last one
if (!added) {
pw.println(word);
}
pw.close();
br.close();
} catch (IOException ioe) {
// still delete the temp file if something happens
tempFile.delete();
throw new WordNotAddedException(
"IOException: " + ioe);
}
}
// clear up
tempFile.delete();
}
/** Check whether the specified word exists in this word list.
* Since binary search is used, this operation is quite efficient
* even for a large list file.
*
* @param word the word to be checked
* @return whether the word exists
* @throws WordListException if exception occurs
*/
public synchronized boolean findWord(String word)
throws WordListException {
if (word == null) {
throw new WordListException();
}
// acquire the lock
synchronized (lock) {
try {
// check whether the first word matches
raf.seek(0);
String str = raf.readLine();
if (str == null) {
return false;
}
if (comparator.compare(word, str.trim()) == 0) {
return true;
}
// binary search starts
long lower = 0;
long upper = raf.length() - 1;
while (lower <= upper) {
long index = (lower + upper) / 2;
raf.seek(index);
// read off an incomplete line
raf.readLine();
str = raf.readLine();
// the line might be null if it's the end of file
int t = str == null ? -1
: comparator.compare(word, str.trim());
// found it ^_^
if (t == 0) {
return true;
}
if (t > 0) {
lower = index + 1;
} else {
upper = index - 1;
}
}
} catch (IOException ioe) {
throw new WordListException("IOException: " + ioe);
}
}
// not found
return false;
}
}