I have an array of integers:
[56, 6090, 1510, 256, 17]
How do I write this into a file in append mode? I'll have many more arrays just like to write to the same file.. how do I append them line by line?
I want my final file to look like this:
A B C D E
56 6090 1510 256 17
60 42 3400 5499 12
How do I get this to work? How do I write the headers?
I have this but my resulting file looks strange. What are those characters?
public static void main(String args[]) throws IOException {
// input files
String fileName = "SmallAreaIncomePovertyEstData.txt";
File f = new File(fileName);
// output files
String tempFileName = "tempFile.txt";
File outputf = new File(tempFileName);
//writer to the output file
BufferedWriter outputWriter = new BufferedWriter(new FileWriter(outputf, true));
BufferedReader br = new BufferedReader(new FileReader(f));
...
for(int i = 0; i < rowDataOnlyIntegers.length; i++) {
outputWriter.write(rowDataOnlyIntegers[i]);
}
outputWriter.newLine();
}
br.close();
outputWriter.close();
I suggest that you use a PrintWriter
to write to a file instead. The method write
that you are calling writes a character encoded as the integer argument, not the string representation of the integer, which you have expected. So write(32)
would write a space character, because a space is encoded as 32 in UTF-16.
PrintWriter
allows you to directly write strings instead.
Here's how to use a PrintWriter
.
String tempFileName = "tempFile.txt";
File outputf = new File(tempFileName);
//writer to the output file
PrintWriter outputWriter = new PrintWriter(outputf);
...
for(int i = 0; i < rowDataOnlyIntegers.length; i++) {
outputWriter.print(rowDataOnlyIntegers[i]);
outputWriter.print(" ");
}
outputWriter.println();
You can use the Java 8 API to manipulate integer arrays and write lines into a file. In my opinion, it's easier and cleaner.
The process of writing all the lines into a file is just one line of code (assuming that you don't load super large amount of data into the memory all at once):
void writeLines(Path file, List<String> lines) throws IOException {
Files.write(file, lines, StandardOpenOption.APPEND);
}
Let's suppose you have a method that provides you with a list of integer arrays:
List<int[]> getIntArrays() {
int[] a1 = { 23, 54, 656, 332, 556 };
int[] a2 = { 12, 45, 6556, 232, 323 };
int[] a3 = { 898, 787, 23, 4545, 233 };
return Arrays.asList(a1, a2, a3);
}
You can convert each array into a one-line string this way:
String toString(int[] array) {
String DELIMITER = " ";
return String.join(DELIMITER, IntStream.of(array)
.mapToObj(a -> String.valueOf(a))
.collect(Collectors.toList()));
}
The next method converts a list of integer arrays into a list of strings:
List<String> toString(List<int[]> arrays) {
return arrays.stream().map(a -> toString(a)).collect(Collectors.toList());
}
This list can be fed to the writeLines
method above and the lines will be written to a file.
How do we write the header?
Here's a full example. It writes the header add all the lines of integers.
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ArrayWriter {
private final static Charset UTF8 = Charset.forName("UTF-8");
private final static String DELIMITER = " ";
public static void main(String[] args) throws IOException {
Path file = Paths.get("test.txt");
writeHeader(file);
writeLines(file, toString(getIntArrays()));
}
private static void writeHeader(Path file) throws IOException {
String line = String.join(DELIMITER, getHeader());
Files.write(file, Arrays.asList(line), UTF8, StandardOpenOption.CREATE);
}
private static void writeLines(Path file, List<String> lines) throws IOException {
Files.write(file, lines, UTF8, StandardOpenOption.APPEND);
}
private static List<String> toString(List<int[]> arrays) {
return arrays.stream().map(a -> toString(a)).collect(Collectors.toList());
}
private static String toString(int[] array) {
return String.join(DELIMITER,
IntStream.of(array).mapToObj(a -> String.valueOf(a)).collect(Collectors.toList()));
}
public static String[] getHeader() {
return new String[] { "A", "B", "C", "D", "E" };
}
public static List<int[]> getIntArrays() {
int[] a1 = { 23, 54, 656, 332, 556 };
int[] a2 = { 12, 45, 6556, 232, 323 };
int[] a3 = { 898, 787, 23, 4545, 233 };
return Arrays.asList(a1, a2, a3);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.