자바 파일 입출력 질문드려요

revol2236의 이미지

제가 만들고 싶은 부분은 기존 파일에 있는 부분에 덧붙여서 다른 내용을 저장하는 것인데요

예를들어 data.txt 라는 텍스트 파일에
kldp.org 라는 내용이 써져있다면 다시한번 프로그램을 실행시킬때는

kldp.org 라는 내용아래에
linux 라는 문자가 추가 되는 형식 입니다,, 그런데 어쩐지 컴파일은 되는데 실행할때

오류가 뜨네요,,

문제가 뭔지 알려주시면 감사하겠습니다,,,

import java.util.StringTokenizer;
import java.io.*;
 
class JAVA_File {
 
public static void main(String[] args){
	try{
			File imsifile = new File("./file_pra/data2.txt");
		String s = "캐릭터 정보";
		String k;
 
			BufferedReader an = new BufferedReader(new FileReader(imsifile));
 
		File filename = new File("./file_pra/data.txt");
		BufferedReader ow = new BufferedReader(new FileReader(filename));
 
		System.out.println(ow.readLine());
		k=ow.readLine();	
		if(k==""){
			BufferedWriter wr = new BufferedWriter(new FileWriter("./file_pra/data.txt"));
			System.out.println(ow.readLine());
			wr.write(s);
			wr.newLine();
			wr.close();
		}
		else{
			BufferedWriter ir = new BufferedWriter(new FileWriter(imsifile));
			do{
				ir.write(ow.readLine());
				ir.newLine();
			}while(ow.readLine()!=null);
			ir.write(s);
			ir.close();
			BufferedWriter wr = new BufferedWriter(new FileWriter("./file_pra/data.txt"));
			do{
				wr.write(an.readLine());
				wr.newLine();
			}while(an.readLine()!=null);
				wr.close();
		}
 
	}catch(Exception ex){
		ex.printStackTrace();
	}
	}
}
 
ssehoony의 이미지

try catch 의 exception의 call stack 및 에러 메시지를 잘 확인해 보세요.
거기에 힌트가 있습니다.

revol2236의 이미지

위엣분 코드도 감사했습니다만

혼자서 여차여차 하다보니까 해결이 됬습니다,,

저는 생성자 호출 부분에서 문제가 있을줄 알았는데

어이없게도 반복문을 잘못쓰는 바람에

readLine() 할때마다 여기에 계속 null 값이 들어 가고 있었더군요 -ㅡ-;;;

관심가져 주셔서 감사합니다 !!!

============================================

비가오면 사람들은 눈물흘리고

그빗물에 흘러내린 눈물감추고

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.