• 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");
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!