728x90
FileInputStream
public class FileExample {
public static void main(String[] args) throws Exception {
}
}
1) FileInputStream은 file객체가 들어간다.
new FileInputStream(file)
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
new FileInputStream(file);
}
}
2) new file에는 파일경로가 들어가는데 이때의 파일경로는 절대경로!
상대경로는 현재 폴더 기준으로 요약해서 써주는것! .는 현재, ..는 부모폴더
절대경로는 전체 경로를 다 써주는것
new FileInputStream(new File("/home/pc24/200513.sql"));
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
new FileInputStream(new File("/home/pc24/200513.sql"));
}
}
3) Assign
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
}
}
4) 4바이트 씩 끊기
byte[] buffer = new byte[4];
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
byte[] buffer = new byte[4];
}
}
5) 만약 읽을 바이트가 없으면(파일의 끝이라면) -1을 리턴해준다.
따라서 -1이 아닐때까지 읽어들인다.
while (inputStream.read(buffer) != -1) {
}
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
byte[] buffer = new byte[4];
while (inputStream.read(buffer) != -1) {
}
}
}
6) buffer는 바이트 타입(숫자)이므로 아래와 같이 출력됨
while (inputStream.read(buffer) != -1) {
System.out.print(buffer);
}
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
byte[] buffer = new byte[4];
while (inputStream.read(buffer) != -1) {
System.out.print(buffer);
}
}
}
7) 바이트인 buffer를 문자로 변환
System.out.print(new String(buffer));
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
byte[] buffer = new byte[4];
while (inputStream.read(buffer) != -1) {
System.out.print(new String(buffer));
}
}
}
8) 자원반납
inputStream.close();
더보기
![](https://blog.kakaocdn.net/dn/IFLHv/btqE41NXuFP/oq5KALM2LpUyIwMls6ldO1/img.png)
public class FileExample {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
byte[] buffer = new byte[4];
while (inputStream.read(buffer) != -1) {
System.out.print(new String(buffer));
}
inputStream.close();
}
}
>> 한글이 깨짐!
4바이트씩 쪼갰기 때문!!
![](https://blog.kakaocdn.net/dn/IFLHv/btqE41NXuFP/oq5KALM2LpUyIwMls6ldO1/img.png)
9) 한글깨짐 해결
쪼개는 것의 기준을 2048바이트로하여 한번에 데이터를 처리하게 했다.
이는 한글깨짐은 해결했지만 메모리 낭비가 심하다
byte[] buffer = new byte[2048];
더보기
public class FileExample {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream(new File("/home/pc24/200513.sql"));
byte[] buffer = new byte[2048];
while (inputStream.read(buffer) != -1) {
System.out.print(new String(buffer));
}
inputStream.close();
}
}
FileOutputStream
public class FileExample {
public static void main(String[] args) throws Exception {
new FileOutputStream(new File("/home/pc24/자바로만든파일.txt"));
}
}
Assign
public class FileExample {
public static void main(String[] args) throws Exception {
FileOutputStream outputStream = new FileOutputStream(new File("/home/pc24/자바로만든파일.txt"));
}
}
버퍼가 필요!
내가 입력한 글자가 버퍼야
outputStream.write("자바로 파일을 만들었어요.");는 string타입
public class FileExample {
public static void main(String[] args) throws Exception {
outputStream.write("자바로 파일을 만들었어요.");
}
}
.getBytes()를 사용하여 바이트단위로 리턴
public class FileExample {
public static void main(String[] args) throws Exception {
outputStream.write("자바로 파일을 만들었어요.".getBytes());
}
}
자원반납
public class FileExample {
public static void main(String[] args) throws Exception {
outputStream.write("자바로 파일을 만들었어요.".getBytes());
outputStream.close();
}
}
JavaFx로 IOStream
main
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class fileIOMain extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("fileIO.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("파일입출력");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller
public class fileIOController {
@FXML
TextArea textArea;
@FXML
public void openAction() throws Exception {
FileChooser chooser = new FileChooser();
File dialog = chooser.showOpenDialog(null);
String path = dialog.getPath();
FileInputStream inputStream = new FileInputStream(new File(path));
byte[] buffer = new byte[2048];
while (inputStream.read(buffer) != -1) {
textArea.setText(new String(buffer));
}
}
// 저장
@FXML
public void storeAction() throws Exception {
FileOutputStream outputStream = new FileOutputStream(new File("/home/pc24/자바로만든파일.txt"));
//버퍼가 필요!
// 내가 입력한 글자가 버야
//outputStream.write("자바로 파일을 만들었어요."); string타입
outputStream.write(textArea.getText().getBytes());//byte단위로 리턴
outputStream.close();
}
@FXML
public void newAction() throws Exception {
textArea.setText("");
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171"
fx:controller="chapter18.fileIOController">
<children>
<Button layoutX="93.0" layoutY="14.0" mnemonicParsing="false" onAction="#openAction" text="파일열기" />
<TextArea fx:id="textArea" layoutX="14.0" layoutY="54.0" prefHeight="332.0" prefWidth="567.0" />
<Button layoutX="263.0" layoutY="14.0" mnemonicParsing="false" onAction="#storeAction" prefHeight="26.0" prefWidth="70.0" text="파일저장" />
<Button layoutX="444.0" layoutY="14.0" mnemonicParsing="false" onAction="#newAction" prefHeight="26.0" prefWidth="70.0" text="새파일" />
</children>
</AnchorPane>