Win a copy of Terraform in Action this week in the Cloud forum!

jwiscou chen

Greenhorn
+ Follow
since Sep 13, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by jwiscou chen

now I know thank you
15 years ago
Thanks for your advice now I modify to follow can anyone help me

15 years ago
when I run follow code have a NullPointerException occured can anyBody help me

Thanks Advance
15 years ago
can anybody tell me why generate follow result.
thanks advance
15 years ago
/* 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 ]
15 years ago
/* 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");
}
}
15 years ago
Hello everyone
I have some doubt follow can anyone suggest why?
1. byte a=0xFF //compile error
byte b=0xFFF //compile error
byte c=0xFFFFFFFF //but here compile succeed why?
2. char a='A';
char b='A'+8; //compile succeed
char c=a+8; //but here compile error why?
Thanks advance
15 years ago