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