Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can't a BufferedReader and a BufferedWriter be open at the same time?

J. NewmanApr 19 2011 — edited May 4 2011
To the experienced:

I am writing a class to selectively extract data from an Excel file, and write out to a plain text file. I am using JDeveloper 11.1.1.3.

It works fine and writes out the output file as expected when reading data from the spreadsheet and directly writing to the output file.

However, I need to convert the data in one of the columns based on a conversion table. The conversion table is a plain text file that has two columns separated by a space. I read in the conversion table using a BufferedReader and put it in a HashMap for use. However, once the Buffered Reader and the HashMap are introduced, the BufferedWriter does not work any more.

The way the class works is that, the main() method (not shown here) reads data from the Excel sheet into a List, and then calls the method showExcelData() (as shown below) to write out data from the List to a plain text file.

When the green color code was not added, the output file was written to the disk fine. A sample line from the output file is:
JOHN:DOE:11223344:12345:20110824
By adding the green code, I expect it to output the line as:
JOHN:DOE:11223344:STUDENT:20110824
The problem is, after the green code is added, the output file the program writes to the disk is zero size.

I added the System.out.println() lines for troubleshooting. These lines show that the BufferedWriter <b>out</b> is not null, and the conversion is actually taking place. But <b>out.write()</b> simply does not work. Commenting out the green code, the program is working again - of course, without date conversion for that column.

There must be something wrong but I can not see where it is wrong.

Your help is very much appreciated!


Newman


<pre>
private static void showExcelData(List sheetData, String outputFilename) throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter(outputFilename));
System.out.println("Is out null? " + (out == null));
BufferedReader in = null;
for (int i = 3; i < sheetData.size(); i++) {
List list = (List)sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
HSSFCell cell = (HSSFCell)list.get(j);
if (j == 5 || j == 6 || j == 7 || j == 11) {
<font color="green"><b>
// The column with index 11 is the column of data that needs conversion:
if (j == 11) {
String convertionFilename = "ConversionTable.txt";
Map<String, String> liveMap =
new HashMap<String, String>();
try {
in = new BufferedReader(new FileReader(convertionFilename));
String line = null;
while ((line = in.readLine()) != null) {
String[] keyValue = line.split(" ");
liveMap.put(keyValue[0], keyValue[1]);
}
} catch (IOException e) {
String message = e.getMessage();
}

String titleCode =
cell.getRichStringCellValue().toString();

String role = liveMap.get(titleCode).toUpperCase();
out.write(role);
System.out.println(titleCode + " " + role);
} else {</b></font>
out.write(cell.getRichStringCellValue().toString().toUpperCase());
System.out.println(cell.getRichStringCellValue().toString().toUpperCase());<font color="green"><b>
}</b></font>
if (j < list.size() - 1 && j != 11) {
out.write(":");
}
} else if (j == 26) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
Calendar cellDate = Calendar.getInstance();
cellDate.setTimeInMillis(cell.getDateCellValue().getTime());
String year = "" + cellDate.get(Calendar.YEAR);
String month =
padding(cellDate.get(Calendar.MONTH) + 1);
String day =
padding(cellDate.get(Calendar.DAY_OF_MONTH));
String expDate = year + month + day;
out.write(":" + expDate);
}
}
}
}
out.write(System.getProperty("line.separator"));
}
if (in != null) in.close();
if (out != null) out.close();
}
</pre>
This post has been answered by EJP on Apr 19 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 1 2011
Added on Apr 19 2011
8 comments
1,658 views