By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,237 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,237 IT Pros & Developers. It's quick & easy.

Java question: How do I read a file into an double array; and then write it out to s

P: 1
Hi, I am doing this homework where I am suppose to read a .txt file fill with int number into a double array. After which I had to write this double array into another .txt file.
At first I though everything was working as I was able to read the file and display it out as an image, and then save it into a .txt file as double. However when I tried to use the same class file for another lesson (display it in frame I created), I keep getting 0.0 as the value of the output .txt file. Below is the code for my read and write class:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class IO {
  7.     FileDialog fd;
  8.     public double[][] readData() { 
  9.         fd = new FileDialog(new Frame(), "Open Files", FileDialog.LOAD);
  10.         fd.setVisible(true);
  11.         File f = null;
  12.         if ((fd.getDirectory() != null)||( fd.getFile() != null)) {
  13.             f = new File(fd.getDirectory() + fd.getFile());
  14.         }
  15.         FileReader fr = null;
  16.         try {
  17.             fr = new FileReader (f);
  18.         } catch (FileNotFoundException fnfe) {
  19.             fnfe.printStackTrace();
  20.         }
  21.         BufferedReader br = new BufferedReader(fr);
  22.         int lines = -1;
  23.         String textIn = " ";
  24.         String[] file = null;
  25.         try {
  26.             while (textIn != null) {
  27.                 textIn = br.readLine();
  28.                 lines++;
  29.             }
  30.             file = new String[lines];
  31.             fr = new FileReader (f);
  32.             br = new BufferedReader(fr);
  33.             for (int i = 0; i < lines; i++) {
  34.                 file[i] = br.readLine();
  35.             }
  36.             br.close();
  37.         } catch (IOException ioe) {
  38.             ioe.printStackTrace();
  39.         }
  40.         double[][] data = new double [lines][];
  41.         for (int i = 0; i < lines; i++) {
  42.             StringTokenizer st = new StringTokenizer(file[i],",");
  43.             data[i] = new double[st.countTokens()];
  44.             int j = 0;
  45.             while (st.hasMoreTokens()) {
  46.                 data[i][j] = Double.parseDouble(st.nextToken());
  47.                 j++;
  48.             }
  49.         }
  50.         return data;
  51.     }
  52.  
  53.  
  54.     public void writeData(double[][] dataIn) {
  55.         fd = new FileDialog(new Frame(), "Save Files", FileDialog.SAVE);
  56.         fd.setVisible(true);
  57.         File f = null;
  58.         if ((fd.getDirectory() != null)||( fd.getFile() != null)) {
  59.             f = new File(fd.getDirectory() + fd.getFile());
  60.         }
  61.         FileWriter fw = null;
  62.         try {
  63.             fw = new FileWriter (f, true);
  64.         } catch (IOException ioe) { 
  65.             ioe.printStackTrace();
  66.         }
  67.         BufferedWriter bw = new BufferedWriter (fw);
  68.         String tempStr = "";
  69.         try {
  70.             for (int i = 0; i < dataIn.length; i++) {
  71.                 for (int j = 0; j < dataIn[i].length; j++) {
  72.                     tempStr = String.valueOf(dataIn[i][j]);
  73.                     bw.write(tempStr);
  74.                 }
  75.                 bw.newLine();
  76.             }
  77.             bw.close();
  78.         } catch (IOException ioe) {
  79.             ioe.printStackTrace();
  80.         }
  81.     }
  82. }
  83.  
Any help will be greatly appreciated.
Jul 1 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.