자바 개발자님들 도와주세요...

익명 사용자의 이미지

자바로 화일입출력관련 프로그램을 짜고 있는 사람입니다.
급하게 낼 모레까지 완성해야 하는데 급한 맘에 이렇게 두서없이 질문을
올리게 되었습니다.
지나가시는 개발자님들 귀찮아 하지마시고 도와주심 감사드리겠습니다.

그럼 대략적인 설명을 드리면..제가 궁극적으로 하고자 하는 것은 입력화
일의 값을 2차원 배열에 읽어들여 그 값을 이용해 어떤 알고리즘을 수행한
뒤 그 결과값을 화일에 출력하려고 하는것인데요...알고리즘은 일단 완성
이 되었는데 입력화일을 2차원 배열에 읽어들이지를 못하고 있습니다.
다음과 같은 입력화일(input.txt)이 있을때요...

0 1 4 999 999 999
1 0 3 4 5 5
999 999 5 7 999 4
6 5 999 7 999 999
2 4 6 9 3 0
0 2 7 9 999 999

제가 원하는 작업은 위의 정수값들을 화일로 부터그대로 topology[6][6]라
는 배열에 입력을 받고 싶은데요...C로는 간단히 구현가능한데 java는 화
일 입출력이 왜이리 복잡한 것인지.(--;)
하여튼 제가 나름 대로 작성한 코드는 아래와 같습니다.
원래 하려고 했던 목표는 위에 6행6열짜리의 데이타를 그대로 6행6열짜리
int 배열에 입력 하는겁니다.그런데 아래와 같이 작성해서 실행을 시키셔
출력해보면 출력값이 위에있는 화일들의 값과는 달리 이상한 값들이 출력
이 됩니다.왜? 그런건지 도와주세요...부탁드립니다.(참고로doc문서에 찾
아보니 DataInputStream의 read메쏘드는 4바이트씩 읽어서 그값을 정수로
해서 리턴한다고 나왔거든요....그래서 아래와 같이 작성 하였답니다.)

그럼 부탁드리겠습니다.

import java.io.*;

class FileRead
{
public static void main(String args[]) throwsException
{
String file_name = "input.txt";
DataInputStream in =newDataInputStream (new FileInputStream
(file_name));
int i,j;
int topology[][] = new int[6][6];

for(i=0; i {
for(j=0; j {
topology[i][j] = in.readInt());
}
}

for(i=0; i {
for(j=0; j {
System.out.print(topology[i][j]+" ");
}
System.out.println();
}

in.close();
}
}

참고로 결과값이 다음과 같이 터무니 없는 값들이 나옵니다.

538976288 807411744 824188960 540287008 538980656 808460320
538980656 808460320 538980656 808455434 538976288 824188960
540024881 808464416 538976288 538981152 538976288 538980640
538976305 808464397 169877536 540287025 808464416 538976304
538976288 825241648 538976288 538976305 538976288 825241648
537725472 825241648 538976288 857747760 808460320 538976288
(6행6열로 나옵니다.)

익명 사용자의 이미지

InputStream.read()는 binary로 data를 읽어 들입니다.

즉 DataInputStream.read()는 4byte를 읽어서 integer로 만들죠...

100 200 300 ...

이렇게 파일에 기록되어 있다고 하면

0x31[1] 0x30[0] 0x30[0] 0x20[space] 0x32 0x30 0x30 0x20 0x33 0x30
0x40 ...

readInt 시에는 0x31 0x30 0x30 0x20 을읽어서 endian에 따라 값을 변환하
게 됩니다.

따라서 당연히 원하는 값인 100, 200, 300이 나오지 않게 됩니다.

님께서는 text를 읽어 그것을 값으로 바꾼후 사용하셔야합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.