FileInputTest1.java
public class FileInputTest1 {
public static void main(String[] args) {
FileInputStream fis=null;
try {
fis= new FileInputStream("input.txt");
int i;
//-1이 아닐때까지 계속 읽음
while((i=fis.read()) != -1) {
System.out.println((char)i);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("end");
}
}
FileInputTest2.java
public class FileInputTest2 {
public static void main(String[] args) {
try(FileInputStream fis=new FileInputStream("input.txt")) {
int i;
//-1이 아닐때까지 계속 읽음
while((i=fis.read()) != -1) {
System.out.println((char)i);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("end");
}
}
FileInputTest3.java
public class FileInputTest3 {
public static void main(String[] args) {
try(FileInputStream fis=new FileInputStream("input2.txt")) {
int i;
//byte단위의 10개의 배열을 만듬
byte[] bs=new byte[10];
//-1이 아닐때까지 계속 읽음
while((i=fis.read(bs)) != -1) {
//버퍼를 읽을때는 garbage가 남기 때문에 모두다 출력됨
// for(byte b : bs) {
// System.out.print((char)b);
// }
for(int k=0;k<i;k++) {
System.out.print((char)bs[k]);
}
System.out.println();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("end");
}
}
FileOutputTest1.java
public class FileOutputTest1 {
public static void main(String[] args) {
//true로 하면 txt파일에 계속 문자가 남게됨
try(FileOutputStream fos= new FileOutputStream("output.txt",true)){
fos.write(65);
fos.write(66);
fos.write(67);
}catch(IOException e) {
System.out.println(e);
}
}
}
FileOutputTest2.java
public class FileOutputTest2 {
public static void main(String[] args) {
byte[] bs=new byte[26];
//아스키코드 숫자65는 A
byte data=65;
for(int i=0;i<bs.length;i++) {
bs[i]=data;
data++;
}
try(FileOutputStream fos= new FileOutputStream("alpha.txt",true);
FileInputStream fis=new FileInputStream("alpha.txt")){
fos.write(bs);
int ch;
while((ch=fis.read()) != -1) {
System.out.print((char)ch);
}
}catch(IOException e) {
System.out.println(e);
}
}
}
FileReaderTest1.java
public class FileReaderTest1 {
public static void main(String[] args) throws IOException {
//FileInputStream은 바이트단위로 읽을 때 사용
//InputStreamReader를 사용하면 바이트단위로 읽은 자료를 문자로 변환해줌
FileInputStream fis=new FileInputStream("reader.txt");
InputStreamReader isr=new InputStreamReader(fis);
int i;
while((i=isr.read()) != -1) {
System.out.print((char)i);
}
fis.close();
}
}
FileReaderTest2.java
public class FileReaderTest2 {
public static void main(String[] args) throws IOException {
//FileReader는 문자를 그대로 가져옴
FileReader fis=new FileReader("reader.txt");
int i;
while((i=fis.read()) != -1) {
System.out.print((char)i);
}
fis.close();
}
}
FileWriterTest1.java
public class FileWriterTest1 {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("writer.txt");
fw.write('A');
char[] buf= {'B','C','D','E','F'};
fw.write(buf);
fw.write("안녕하세요");
//2번째'D'에서 2개만큼
fw.write(buf,2,2);
fw.close();
System.out.println("end");
}
}
writer.txt
ABCDEF안녕하세요DE
FileCopy.java
public class FileCopy {
public static void main(String[] args) throws IOException {
long milliseconds=0;
try(FileInputStream fis=new FileInputStream("a.zip");
FileOutputStream fos=new FileOutputStream("copy.zip");
//버퍼를 이용하면 복사하는 속도가 훨씬 빠름
BufferedInputStream bis= new BufferedInputStream(fis);
BufferedOutputStream bos= new BufferedOutputStream(fos)){
milliseconds=System.currentTimeMillis();
int i;
while((i=fis.read()) != -1 ) {
fos.write(i);
}
milliseconds=System.currentTimeMillis()-milliseconds;
} catch (IOException e) {
System.out.println(e);
}
Socket socket=new Socket();
//inputstreamreader로 문자변환해주고 bufferedreader로 속도변환해줌
BufferedReader isr=new BufferedReader(new InputStreamReader(socket.getInputStream()));
isr.readLine();
System.out.println("시간 : " + milliseconds);
}
}
DataStreamTest.java
public class DataStreamTest {
public static void main(String[] args) {
try(FileOutputStream fos=new FileOutputStream("data.txt");
DataOutputStream dos=new DataOutputStream(fos);
FileInputStream fis=new FileInputStream("data.txt");
DataInputStream dis=new DataInputStream(fis)){
dos.writeByte(100);
dos.write(100);
dos.writeChar('A');
dos.writeUTF("안녕하세요");
System.out.println(dis.readByte());
System.out.println(dis.read());
System.out.println(dis.readChar());
System.out.println(dis.readUTF());
}catch(IOException e) {
}
}
}