I am writing integers from 1-100 into a file separated by commas.
I need to know how to read in a "," into a file and print it out again.
Below the code is sample output after execution of the code. The comma is not output correctly. I know it has to do with the filestream.

import java.io.*;
import java.util.*;


public class FileProcessing {
    //private static DataOutputStream output;
    //private static FileInputStream input;
    private static ArrayList inputList;

    public static void main(String[] args) throws IOException {
        createOutputFile();
        createInputFile();
        printInputList();
    }

    private static void createOutputFile () throws IOException {
        FileOutputStream fos = new FileOutputStream("numbers.dat");
        DataOutputStream dos = new DataOutputStream(fos);
        for (int i=1; i<=100; i++){
            dos.write(i);
            if (i != 100){
              dos.write(',');
            }
        }
        dos.close();
        }

    private static void createInputFile ()  {
        try {
        FileInputStream fis = new FileInputStream("numbers.dat");
        DataInputStream dis = new DataInputStream(fis);

        inputList = new ArrayList<Integer>(10);
        int value;
        
            while ((value = dis.read()) != -1) {
                inputList.add(value);
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private static void printInputList() {
       String dataOutput;
       if (inputList.size() > 0) {
           dataOutput = "input values are: \n";
           for (int i=0; i < inputList.size(); i++) {
                   dataOutput += inputList.get(i);
               }
           }
       else {
           dataOutput = "no values in input List";
       }
       System.out.println(dataOutput);
    }
}

input values are:
14424434444454464474484494410

I think that ',' is interpreted as a char literal and therefore will be treated as an integer using the comma's unicode value of decimal 44....

If I'm correct then what about outputting the comma as a String literal instead, i.e. ","

If that's not correct then what about using an escape sequence for comma instead, like "\,"

If not that, then maybe using a wrapper?
Does that help?

I have tried several things to get it to recognize the comma as a comma including those you have mentioned. I think what I need to do is convert it back to a readable character in the output but I don't know how to convert it back. I haven't found anything to do this.
The DataOutputStream is acting as a wrapper.

try this in yout printInputList() method :

for (int i=0; i < inputList.size(); i++) {
        	   if(inputList.get(i).equals(new Integer(44)))
                   dataOutput += ",";
        	   else
        		   dataOutput += inputList.get(i);
               }
           }

the would simply compare the input values to 44 (which is the character encoding for the comma) and append a comma to the output if found. otherwise it appends the int value.

by the way, why not use a FileReader or FileWriter instead?

I initally tried the FileWriter but had problems with that as well. The read/write I/O is still confusing. How would I use the FileReader and FileWriter instead? I thought I needed a wrapper for the file stream.

Thanks for the solution; I went down that road but didn't make the
"44" connection.

Another problem occurred when using the above code you recommended, it omits the integer 44 from the list. Here is partial output from the executed code.
40,41,42,43,,,45,46,47,48,49,50

here's a tutorial and an example

http://www.java-samples.com/showtutorial.php?tutorialid=392

Anyways, just to let you know what you did wrong in your code:
1. when you write the int values, by using the write() method you actually was writing the byte values of the ints... you will have trouble once your int values go over 255 (try it). On this note, since you used data streams you could have used writeInt()
2. Same as writing the comma. you could have used writeChar(',') or writeChars(","). read the API for more information on this
3. Now when you read your data file, you were reading the bytes by using read() therefore what is stored in your arraylist are the byte values hence you get 44 for the comma.
4. those problems above was carried over in your printInputList() method

Yes I realize where my problem is however, I don't see the solution. All the FileSteam examples use string or integers. I have not seen where a special character (+,*,&) is maintained in the read/write to file. This seems to be the case with FileReader and FileWriter as well. Am I missing something about that?

can anyone offer any suggestions how to handle special characters in the data stream with integers?

Try treating everything as a String. Example:

String PLUS= "+"; // <- Note the double quotes signifying a String.
for(int i=0;i<50;i++){
    dos.write(Integer.toString(i)); // Convert the int to a String value.
    if(i!=49) dos.write(PLUS); // Write in a + between each int
}

file would look like :
0+1+2+3+4+5....+49

I hope this helps =)

Edited 7 Years Ago by llemes4011: forgot closing bracket

When I print it out will I have to convert back to integer to have everything print correctly and the + displayed right?

This article has been dead for over six months. Start a new discussion instead.