Skip to content

Commit

Permalink
add io file
Browse files Browse the repository at this point in the history
  • Loading branch information
clues committed Nov 16, 2011
1 parent 29fa58c commit 6134ab8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ctalk/src/com/db/letdb/file/InWrite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.db.letdb.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
* @created: 2011-11-16
* @author : jias chao<[email protected]>
*/
public class InWrite {

private String path = "";
private String fileName="";
private BufferedReader read;
private BufferedWriter write;

public static int indexOfLine = 0;

public InWrite(String _fileName) throws IOException{
this.fileName = _fileName;
openFile();
}

public void openFile() throws IOException{
File file = new File(this.fileName);
if (!file.exists()){
file.createNewFile();
}
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file,true);
read = new BufferedReader(fr);
write = new BufferedWriter(fw);
}

public void write(String context) throws IOException{
write.write(context);
write.newLine();
write.flush();
}

public String readNextLine() throws IOException{
read.skip(indexOfLine);
indexOfLine = indexOfLine + 5;
return read.readLine();
}
}
36 changes: 36 additions & 0 deletions ctalk/test/com/db/letdb/file/TestInWrite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.db.letdb.file;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.Test;

/**
* @created: 2011-11-16
* @author : jias chao<[email protected]>
*/
public class TestInWrite {

@Test
public void test() {
fail("Not yet implemented");
}

@Test
public void testWrite() throws IOException{
InWrite iw = new InWrite("context.txt");
iw.write("abc");
iw.write("123");

}

@Test
public void testRead() throws IOException{
InWrite iw = new InWrite("context.txt");
String str = iw.readNextLine();
System.out.print(str);

}

}

0 comments on commit 6134ab8

Please sign in to comment.