--- id: java-fileinputstream title: "Java FileInputStream" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["μžλ°” FileInputStream"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["java", "programming", "w3schools", "io", "fileinputstream", "byte-stream"] raw_sources: ["https://www.w3schools.com/java/java_fileinputstream.asp"] applied_in: [] github_commit: "" --- # [[Java FileInputStream]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `FileInputStream.read()` returns `-1` (not an exception or null) to signal end-of-file β€” reading one raw byte at a time, this integer sentinel value is the loop-termination condition (`while ((i = input.read()) != -1)`), and any file type can be copied byte-for-byte using this same mechanism regardless of content. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`FileInputStream`** β€” reads raw bytes, giving full control beyond what Scanner offers; essential for binary data (images, audio, PDFs). [S1] - **`.read()`** β€” returns one byte as an int, or `-1` at end-of-file. [S1] - **Byte-to-char casting** β€” `(char) i` converts a byte to a printable character for text files. [S1] - **Universal file copying** β€” pairing `FileInputStream` (read) with `FileOutputStream` (write) can copy ANY file type since it operates on raw bytes, not interpreted text. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Read text byte-by-byte: `try (FileInputStream input = new FileInputStream("filename.txt")) { int i; while ((i = input.read()) != -1) { System.out.print((char) i); } } catch (IOException e) {...}`. [S1] - Copy a binary file: `try (FileInputStream input = new FileInputStream("image.jpg"); FileOutputStream output = new FileOutputStream("copy.jpg")) { int i; while ((i = input.read()) != -1) { output.write(i); } }`. [S1] - Class choice guide: Scanner (simple text/parsing), BufferedReader (large text, line-by-line), FileInputStream (binary/raw byte control). [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 이미지 파일(image.jpg β†’ copy.jpg)을 λ°”μ΄νŠΈ λ‹¨μœ„λ‘œ λ³΅μ‚¬ν•˜λŠ” μ˜ˆμ œκ°€ λ°”μ΄λ„ˆλ¦¬ 데이터 처리의 λŒ€ν‘œ μ‹€μ „ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Copying any file type byte-by-byte (Java): ```java try (FileInputStream input = new FileInputStream("image.jpg"); FileOutputStream output = new FileOutputStream("copy.jpg")) { int i; while ((i = input.read()) != -1) { output.write(i); } System.out.println("File copied successfully."); } catch (IOException e) { System.out.println("Error handling file."); } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[Java Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[Java IO Streams]], [[Java FileOutputStream]], [[Java Files Read]] - **μ°Έμ‘° λ§₯락:** λ°”μ΄νŠΈ λ‹¨μœ„ 파일 읽기 β€” FileOutputStreamκ³Ό 짝을 이루어 파일 볡사에 μ‚¬μš©. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” Java FileInputStream β€” https://www.w3schools.com/java/java_fileinputstream.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "Java FileInputStream" page (Astra wiki-curation, P-Reinforce v3.1 format).