|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.upc.dama.dex.core.TextStream
public class TextStream
Stream to read or write Value.TEXT attributes.
Here there is an example about how to use it:
import edu.upc.dama.dex.core.*;
import edu.upc.dama.dex.io.*;
import java.io.*;
public class TextStreamSample {
public static void main(String[] args) throws FileNotFoundException, IOException {
DEX dex = new DEX();
GraphPool gpool = null;
Session sess = null;
DbGraph dbg = null;
int doc = Graph.INVALID_TYPE;
long id = Graph.INVALID_ATTRIBUTE;
long text = Graph.INVALID_ATTRIBUTE;
String [] line = null;
Value v = new Value();
//
// Load data
//
gpool = dex.create("abstracts.dex");
sess = gpool.newSession();
dbg = sess.getDbGraph();
doc = dbg.newNodeType("DOC");
id = dbg.newAttribute(doc, "ID", Value.LONG);
text = dbg.newAttribute(doc, "TEXT", Value.TEXT, Graph.ATTR_KIND_BASIC);
CSVReader csvr = new CSVReader(new File("abstracts_ES.txt"), '|');
line = null;
System.out.println("LOADING DOCS...");
while ((line = csvr.readNext()) != null) {
long oid = dbg.newNode(doc);
v.setLong(Long.parseLong(line[0]));
dbg.setAttribute(oid, id, v);
TextStream tstrm = new TextStream();
v.setTextStream(tstrm);
dbg.setAttribute(oid, text, v);
tstrm.write(line[1].toCharArray(), 0, line[1].length());
tstrm.close();
}
csvr.close();
gpool.dumpStorage(sess, "abstracts.storage");
sess.close();
gpool.close();
//
// Retrieve data
//
CSVWriter csvw = new CSVWriter(new FileWriter("tmp.txt"), '|');
line = new String[2];
gpool = dex.open("abstracts.dex");
sess = gpool.newSession();
dbg = sess.getDbGraph();
doc = dbg.findType("DOC");
id = dbg.findAttribute(doc, "ID");
text = dbg.findAttribute(doc, "TEXT");
Objects objs = dbg.select(doc);
Objects.Iterator it = objs.iterator();
System.out.println("RETRIEVING DOCS...");
while (it.hasNext()) {
long oid = it.next();
dbg.getAttribute(oid, id, v);
line[0] = v.toString();
dbg.getAttribute(oid, text, v);
TextStream tstrm = v.getTextStream();
StringBuffer sb = new StringBuffer();
int length = 0;
char[] buff = new char[200];
int len = 0;
while ((len = tstrm.read(buff, 0, 200)) > 0) {
sb.append(buff, 0, len);
}
tstrm.close();
line[1] = sb.toString();
csvw.writeNext(line);
}
it.close();
objs.close();
csvw.close();
sess.close();
gpool.close();
//
// End
//
System.out.print("END!");
dex.close();
}
}
| Nested Class Summary | |
|---|---|
static class |
TextStream.TextReader
Reader implementation for TextStream. |
static class |
TextStream.TextWriter
Writer implementation for TextStream. |
| Constructor Summary | |
|---|---|
TextStream()
Creates a new instance of TextStream. |
|
TextStream(boolean append)
Creates a new instance of TextStream. |
|
| Method Summary | |
|---|---|
void |
close()
Closes the stream. |
void |
flush()
Operation not supported! |
int |
read(char[] cbuf,
int off,
int len)
Reads a maximum of count characters from the current stream into buffer, beginning at index. |
void |
write(char[] cbuf,
int off,
int len)
Writes an array of characters to the given stream. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public TextStream(boolean append)
TextStream.
User will only be able to create just write TextStream instance by means of this constructor.
append - In case of existing data, data will be added if
true and replaced otherwise.public TextStream()
TextStream.
The stream will replace all existing data.
| Method Detail |
|---|
public int read(char[] cbuf,
int off,
int len)
throws java.io.IOException
cbuf - The character array which, when the method returns, contains
the characters read from the current source between off and
(off + len - 1).off - The index of buffer at which to begin writing.len - The maximum number of characters to read.
java.io.IOException
public void write(char[] cbuf,
int off,
int len)
throws java.io.IOException
cbuf - A character array containing the data to write.off - The index into buffer at which to begin writing.len - The number of characters to write from buffer.
java.io.IOException
public void flush()
throws java.io.IOException
java.io.IOException
public void close()
throws java.io.IOException
Once a stream has been closed, further method invocations will
throw an IOException.
Closing a previously-closed stream, however, has no effect.
close in interface java.io.Closeablejava.io.IOException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||