이것도 아니죠?ㅋㅋ..다시 수정할게요..ㅋㅋ
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
public class Part {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
FileChannel InputChannel = null;
long filesize = 0;
int shareNum = 0;
int index = 0;
shareNum = 4;
try {
RandomAccessFile f = new RandomAccessFile(filename,"rw");
InputChannel = f.getChannel();
filesize = f.length();
index = (int) (filesize/shareNum);
int part1 = index+1;
int part2 = index*2+1;
int part3 = index*3+1;
ByteBuffer buf1 = ByteBuffer.allocate(index+1);
InputChannel.position(0);
InputChannel.read(buf1);
buf1.flip();
byte[] by = new byte[buf1.limit()];
buf1.get(by);
System.out.println("1번째 읽어온 부분:" + new String(by));
ByteBuffer buf2 = ByteBuffer.allocate(index);
InputChannel.position(part1);
InputChannel.read(buf2);
buf2.flip();
byte[] by2 = new byte[buf2.limit()];
buf2.get(by2);
System.out.println("2번째 읽어온 부분:" + new String(by2));
ByteBuffer buf3 = ByteBuffer.allocate(index);
InputChannel.position(part2);
InputChannel.read(buf3);
buf3.flip();
byte[] by3 = new byte[buf3.limit()];
buf3.get(by3);
System.out.println("3번째 읽어온 부분:" + new String(by3));
ByteBuffer buf4 = ByteBuffer.allocate(index+1);
InputChannel.position(part3);
InputChannel.read(buf4);
buf4.flip();
byte[] by4 = new byte[buf4.limit()];
buf4.get(by4);
System.out.println("4번째 읽어온 부분:" + new String(by4));
InputChannel.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Friday, August 17, 2007
Thursday, August 16, 2007
부분 읽기/쓰기
파일을 읽기/쓰기 하는 것을 부분적으로 하려면,
1. 파일 크기를 읽어온 다음,
2. 파일 크기를 일정 단위로 나눈다. 예) 1024KB/256KB = 4
3. 그러면 읽어올 대상이 생긴다.
예) 4조각 ->
index 1: 0~256,
index 2: 257~512,
index 3: 513~ 768,
index 4: 769~1024
4. 읽기를 수행한 뒤에 콘솔에 프린트한다.
쓰기는 읽기 반대로 하면 된다.
1. 파일 크기를 읽어온 다음,
2. 파일 크기를 일정 단위로 나눈다. 예) 1024KB/256KB = 4
3. 그러면 읽어올 대상이 생긴다.
예) 4조각 ->
index 1: 0~256,
index 2: 257~512,
index 3: 513~ 768,
index 4: 769~1024
4. 읽기를 수행한 뒤에 콘솔에 프린트한다.
쓰기는 읽기 반대로 하면 된다.
Wednesday, August 15, 2007
부분읽기
얼만큼 읽어와야하는지 length를 정하는 방법을 몰라서
버퍼사이즈를 읽어올만큼만 할당해서 정해놓은 position에서
버퍼사이즈만큼만 읽어옴..-이렇게 짜는거 아니죠?ㅋㅋㅋ
그리고요.. 한글을 인식못해요..숫자랑 영어만..ㅋㅋ
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
public class RandomAccess {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
String filename2 = "C:\\Documents and Settings\\mina\\test\\Random\\src\\f.txt";
FileChannel outputChannel = null;
FileChannel InputChannel = null;
try {
RandomAccessFile f = new RandomAccessFile(filename,"rw");
RandomAccessFile f2 = new RandomAccessFile(filename2,"rw");
InputChannel = f.getChannel();
outputChannel = f2.getChannel();
int BufferSize = 5;
int BufferStart = 3;
ByteBuffer buf = ByteBuffer.allocate(BufferSize);
InputChannel.position(BufferStart);
InputChannel.read(buf);
buf.flip();
outputChannel.position(BufferStart);
outputChannel.write(buf);
buf.flip();
byte[] by = new byte[buf.limit()];
buf.get(by);
System.out.println(new String(by));
outputChannel.close();
InputChannel.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
버퍼사이즈를 읽어올만큼만 할당해서 정해놓은 position에서
버퍼사이즈만큼만 읽어옴..-이렇게 짜는거 아니죠?ㅋㅋㅋ
그리고요.. 한글을 인식못해요..숫자랑 영어만..ㅋㅋ
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
public class RandomAccess {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
String filename2 = "C:\\Documents and Settings\\mina\\test\\Random\\src\\f.txt";
FileChannel outputChannel = null;
FileChannel InputChannel = null;
try {
RandomAccessFile f = new RandomAccessFile(filename,"rw");
RandomAccessFile f2 = new RandomAccessFile(filename2,"rw");
InputChannel = f.getChannel();
outputChannel = f2.getChannel();
int BufferSize = 5;
int BufferStart = 3;
ByteBuffer buf = ByteBuffer.allocate(BufferSize);
InputChannel.position(BufferStart);
InputChannel.read(buf);
buf.flip();
outputChannel.position(BufferStart);
outputChannel.write(buf);
buf.flip();
byte[] by = new byte[buf.limit()];
buf.get(by);
System.out.println(new String(by));
outputChannel.close();
InputChannel.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
RandomAccessFile - file write
1번:RandomAccessFile 이용해서 파일쓰기(Hello World!!추가하기)
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
public class RandomAccess {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
FileChannel outputChannel = null;
try {
String s = "Hello World!!";
byte[] by = s.getBytes();
ByteBuffer buf = ByteBuffer.allocate(by.length);
buf.put(by);
buf.clear();
RandomAccessFile f = new RandomAccessFile(filename,"rw");
outputChannel = f.getChannel();
outputChannel.position(f.length());
outputChannel.write(buf);
outputChannel.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
public class RandomAccess {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
FileChannel outputChannel = null;
try {
String s = "Hello World!!";
byte[] by = s.getBytes();
ByteBuffer buf = ByteBuffer.allocate(by.length);
buf.put(by);
buf.clear();
RandomAccessFile f = new RandomAccessFile(filename,"rw");
outputChannel = f.getChannel();
outputChannel.position(f.length());
outputChannel.write(buf);
outputChannel.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
RandomAccessFile-method & StringTokenzer
* 파일 이름 전송하기 *
StringTokenizer class 이용
< API >:
http://blog.naver.com/wildxing?Redirect=Log&logNo=20037023803
import java.util.*;
캐릭터 라인을 토큰에 분할할 수 있음.
< 생성 >
StringTokenizer(String str)
StringTokenizer(String str,String delim)
delim : 단락 문자
< Method >
int countTokens() : 예외를 생성하지 않고 Tokenizer의 nextToken메소드를 호출할수 있는 회수를 계산
boolean hasMoreTokens() : Tokenizer의 캐릭터라인으로 이용할 수 있는 토큰이 아직 있는지 판정
boolean hasMoreElements() : hasMoreToken 메소드와 같은 값을 돌려줌
String nextToken() : 캐릭터라인 Tokenizer로부터 다음의 토큰을 돌려줌
<파일 이름 가져오기>
byte buf = new byte[512];
StringTokenizer strToken = new StringTokenizer(new String(buf, "KSC5601").trim());
//trim()은 앞뒤 공백제거,문장 중간의 공백은 제거하지 않음/
String header = strToken.nextToken();
String filename = strToken.nextToken();
*RandomAccessFile*
< API >: http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html
< 생성 >RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
< Method >
void close()
FileChannel getChannel()
FileDescriptor getFD()
long getFilePointer()
long length()
int read()
int read(byte[] b)
int read(byte[] b,int off,int len)
boolean readBoolean()
byte readByte()
char readChar()
double readDouble()
float readFloat()
void readFully(byte[] b)
void readFully(byte[] b, int off, int len)
int readInt()
String readLine()
long readLong()
short readShort()
int readUnsignedByte()
int readUnsignedShort()
String readUTF()
void seek(long pos)
void setLength(long newLength)
int skipBytes(int n)
void write(byte[] b)
void write(byte[] b, int off, int len)
void write(int b)
void writeBoolean(boolean v)
void writeByte(int v)
void writeBytes(String s)
void writeChar(int v)
void writeChars(String s)
void writeDouble(double v)
void writeFloat(float v)
void writeInt(int v)
void writeLong(long v)
void writeShort(int v)
void writeUTF(String str)
StringTokenizer class 이용
< API >:
http://blog.naver.com/wildxing?Redirect=Log&logNo=20037023803
import java.util.*;
캐릭터 라인을 토큰에 분할할 수 있음.
< 생성 >
StringTokenizer(String str)
StringTokenizer(String str,String delim)
delim : 단락 문자
< Method >
int countTokens() : 예외를 생성하지 않고 Tokenizer의 nextToken메소드를 호출할수 있는 회수를 계산
boolean hasMoreTokens() : Tokenizer의 캐릭터라인으로 이용할 수 있는 토큰이 아직 있는지 판정
boolean hasMoreElements() : hasMoreToken 메소드와 같은 값을 돌려줌
String nextToken() : 캐릭터라인 Tokenizer로부터 다음의 토큰을 돌려줌
<파일 이름 가져오기>
byte buf = new byte[512];
StringTokenizer strToken = new StringTokenizer(new String(buf, "KSC5601").trim());
//trim()은 앞뒤 공백제거,문장 중간의 공백은 제거하지 않음/
String header = strToken.nextToken();
String filename = strToken.nextToken();
*RandomAccessFile*
< API >: http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html
< 생성 >RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
< Method >
void close()
FileChannel getChannel()
FileDescriptor getFD()
long getFilePointer()
long length()
int read()
int read(byte[] b)
int read(byte[] b,int off,int len)
boolean readBoolean()
byte readByte()
char readChar()
double readDouble()
float readFloat()
void readFully(byte[] b)
void readFully(byte[] b, int off, int len)
int readInt()
String readLine()
long readLong()
short readShort()
int readUnsignedByte()
int readUnsignedShort()
String readUTF()
void seek(long pos)
void setLength(long newLength)
int skipBytes(int n)
void write(byte[] b)
void write(byte[] b, int off, int len)
void write(int b)
void writeBoolean(boolean v)
void writeByte(int v)
void writeBytes(String s)
void writeChar(int v)
void writeChars(String s)
void writeDouble(double v)
void writeFloat(float v)
void writeInt(int v)
void writeLong(long v)
void writeShort(int v)
void writeUTF(String str)
RandomAccessFile--Channel
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Read_Donhak {
public static void main(String[] args) throws FileNotFoundException {
File aFile = new File("c:\\Donhak.txt");
RandomAccessFile inFile = new RandomAccessFile(aFile, "rw");
// 파일 채널 객체
FileChannel inChannel = inFile.getChannel();// ByteBuffer 생성
ByteBuffer buf = ByteBuffer.allocate(1024);
try {
inChannel.read(buf);
byte[] add = buf.array();
String ttt = new String(add);
System.out.println(ttt);
} catch (IOException e) {
e.printStackTrace();
}
buf.clear();
System.exit(0);
}
}
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Read_Donhak {
public static void main(String[] args) throws FileNotFoundException {
File aFile = new File("c:\\Donhak.txt");
RandomAccessFile inFile = new RandomAccessFile(aFile, "rw");
// 파일 채널 객체
FileChannel inChannel = inFile.getChannel();// ByteBuffer 생성
ByteBuffer buf = ByteBuffer.allocate(1024);
try {
inChannel.read(buf);
byte[] add = buf.array();
String ttt = new String(add);
System.out.println(ttt);
} catch (IOException e) {
e.printStackTrace();
}
buf.clear();
System.exit(0);
}
}
Tuesday, August 14, 2007
NIO Examples
1. NIO Server/Connect Examples
http://java.sun.com/developer/JDCTechTips/2004/tt0914.html
2. Java Examples - Network Protocol Examples
http://www.java2s.com/Code/Java/Network-Protocol/CatalogNetwork-Protocol.htm
3. File Name Transmission Example
http://kin.naver.com/db/detail.php?d1id=1&dir_id=10106&eid=LwMOMQ5UhFCvbcXQ7DnYS8kpNd4Cz1G1&qb=wNq52SDGxMDPx+y09SDA/Lzb
4. RandomAccessFile Example
http://java.sun.com/developer/technicalArticles/Streams/WritingIOSC/index.html
5. File Name & Length Transmission Example
http://java.sun.com/developer/TechTips/txtarchive/2004/Apr04_JohnZ.txt
여기서 찾아서 해결할 문제들을 모두 해결해라....
http://java.sun.com/developer/JDCTechTips/2004/tt0914.html
2. Java Examples - Network Protocol Examples
http://www.java2s.com/Code/Java/Network-Protocol/CatalogNetwork-Protocol.htm
3. File Name Transmission Example
http://kin.naver.com/db/detail.php?d1id=1&dir_id=10106&eid=LwMOMQ5UhFCvbcXQ7DnYS8kpNd4Cz1G1&qb=wNq52SDGxMDPx+y09SDA/Lzb
4. RandomAccessFile Example
http://java.sun.com/developer/technicalArticles/Streams/WritingIOSC/index.html
5. File Name & Length Transmission Example
http://java.sun.com/developer/TechTips/txtarchive/2004/Apr04_JohnZ.txt
여기서 찾아서 해결할 문제들을 모두 해결해라....