• Post Reply Bookmark Topic Watch Topic
  • New Topic

I/O question  RSS feed

 
jwiscou chen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* I want to write a program that function: first input one or more name and address from keyboard. and append to end of a file, second write the index of name and address to another file,the index file contain everybody's second name , and the position of the name and address in the first file. using keyboard to input the second name to search corresponding name and address */

//follow is my program, but I can't get the answer. can anybody tell my where error ?
//thanks advance



import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class ReadAndWrite {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long position = 0;
RandomAccessFile file = null;
RandomAccessFile indexFile = null;
FileChannel channel = null;
FileChannel indexChannel = null;

public ReadAndWrite() {
try {
file = new RandomAccessFile("chen.bin","rwd");
indexFile = new RandomAccessFile("index.bin","rwd");
channel = file.getChannel();
indexChannel = indexFile.getChannel();
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
ReadAndWrite s = new ReadAndWrite();
try {
s.selectReadOrWrite();
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
private void selectReadOrWrite() throws IOException {
while(true) {
System.out.println("input \"w\" to inter a new name and address,input \"f\" to search a name,and input \"q\" to quit the program.");
switch(inputStr()) {
case 'w' : writeToFile(); break;
case 'f' : foundIndex(); break;
case 'q' : file.close(); channel.close(); indexFile.close(); indexChannel.close(); System.exit(0); break;
default : System.out.println("Invalid input,try again");
}
}
}
private char inputStr() {
String str = "";
try {
str = br.readLine();
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
return str.charAt(0);
}
private void writeToFile() throws IOException {
System.out.println("please input the first name:");
String firstName = br.readLine().trim();
System.out.println("please onput the second name:");
String secondName = br.readLine().trim();
System.out.println("please input the address:");
String address = br.readLine().trim();
String str = firstName+" "+secondName+" "+address;
ByteBuffer buf1 = ByteBuffer.allocate(2*str.length());
buf1.asCharBuffer().put(str);
//buf1.position(0);
channel.write(buf1);
ByteBuffer buf2 = ByteBuffer.allocate(2*secondName.length()+20);
buf2.putInt(2*secondName.length());
char[] ch = secondName.toCharArray();
for(char c : ch) {
buf2.putChar(c);
}
buf2.putLong(position);
buf2.putLong(2*str.length());
buf2.flip();
indexChannel.position(indexChannel.size()); //ensure buf2 append to end
indexChannel.write(buf2);
position += 2*str.length();
}
private void foundIndex() throws IOException {
System.out.println("please input the second name:");
String secondName = br.readLine().trim();
indexChannel.position(0); // ensure index from first
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.position(buf.limit());
int length = 0;
byte[] strChars = null;
while(true) {
if(buf.remaining() < 4) {
if(indexChannel.read(buf.compact()) == -1)
break;
buf.flip();
}
length = buf.getInt();
if(buf.remaining() < length) {
if(indexChannel.read(buf.compact()) == -1)
assert false;
buf.flip();
}
strChars = new byte[length];
buf.get(strChars);
String name = ByteBuffer.wrap(strChars).asCharBuffer().toString().trim();
if(buf.remaining() < 16) {
if(indexChannel.read(buf.compact()) == -1)
assert false;
buf.flip();
}
if(secondName.equals(name)) {
long file_position = buf.getLong();
long file_len = buf.getLong();
ByteBuffer file_buf = ByteBuffer.allocate((int)file_len);
channel.read(file_buf, file_position);
System.out.println(file_buf.asCharBuffer());
return;
} else {
buf.position(buf.position() + 16);
}
}
System.out.println("no found");
}
}
[Email]http://notepad.mail.yahoo.com/YYY,04a503/srt,0/?v=161&pv=164&CID=-1[/Email]
[ October 01, 2006: Message edited by: jwiscou chen ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you tell us what the problem is? does this compile? do you know how to compile it?

does it run at all or crash? what does it do differently than what you expect?

few people here have the time to figure all that out - and some of it we can't figure out (like the part about what you THINK it should be doing).

we're here to HELP you, not do the work for you. so, tell us exactly where you need help.

also, if you would put the code tags around your post, it preserves the formatting and makes your code much easier to read. before you paste in your code, press that little button in the "instant UBB Code" section. this should put a "[ code]" and "[ /code]" label (without the spaces) in your post. then paste your formatted code BETWEEN them, and what you have will be much easier to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!