바이트 흐름
바이트 입력 흐름 1
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* @author -
* @version :2019 6 12 9:11:13
* :
* 1
* 1.
* 2.
* 3.
* 4.
*
*/
public class IOTest01 {
public static void main(String[] args) {
//1.
File src = new File("abc.txt");
//2.
try {
InputStream is = new FileInputStream(src);
//3. ( )
int data1 = is.read();// h
int data2 = is.read();// e
int data3 = is.read();// l
int data4 = is.read();// ???
System.out.println((char)data1);//h
System.out.println((char)data2);//e
System.out.println((char)data3);//l
System.out.println(data4);// -1
//4.
is.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
파일 바이트 입력 흐름 2
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* @author -
* @version :2019 6 12 9:21:53
* 2
* 1.
* 2.
* 3.
* 4.
*
*/
public class IOTest02 {
public static void main(String[] args) {
// 1.
File src = new File("abc.txt");
// 2.
InputStream is = null;
try {
is = new FileInputStream(src);
// 3. ( )
int temp;
while ((temp = is.read()) != -1) {
System.out.print((char) temp);// hel
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 4.
try {
if (is != null) {//
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
파일 바이트 입력 흐름 3
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* @author -
* @version :2019 6 12 9:52:11
*
* 3
* 1. 2. 3. 4.
*/
public class IOTest03 {
public static void main(String[] args) {
// 1.
File src = new File("abc.txt");
// 2.
InputStream is = null;
try {
is = new FileInputStream(src);
// 3. ( )
////
byte[] flush = new byte[1024];//
int len = -1;//
while ((len = is.read(flush)) != -1) {
//
String str = new String(flush, 0, len);
System.out.println(str);// hello world
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.
try {
if (is != null) {//
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
파일 바이트 출력 흐름
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author -
* @version :2019 6 12 10:06:52
*
* 1.
* 2.
* 3. ( )
* 4.
*
*
*
*/
public class IOTest04 {
public static void main(String[] args) {
//1.
File src = new File("newdesk.txt");// //input
//2.
OutputStream os = null;
try {
os = new FileOutputStream(src);// ,true
//3. ( )
String msg = "hahahaha";
byte[] datas = msg.getBytes();// >>
os.write(datas, 0, datas.length);
os.flush();//
} catch (Exception e) {
}finally {
//
try {if (os!=null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}