The problem is near the end - "Files.write(file1, charArray);" - only prints 1 line to the file. Tried 3 different ways of printing to file. This must be an error with Java.
import java.io.*; import java.nio.file.*; import java.nio.charset.Charset; public class alphabetizer { public static void main(String[] args) { File file = new File("C:\\Documents and Settings\\Administrator\\My Documents\\unsorted_list.txt"); File file2 = new File("C:\\Documents and Settings\\Administrator\\My Documents\\sorted_list.txt"); StringBuffer contents = new StringBuffer(); BufferedReader reader = null; BufferedReader reader1 = null; int count = 0; int listlength = 0; String[] list = null; String temp1 = null; Path file1 = FileSystems.getDefault().getPath("C:\\Documents and Settings\\Administrator\\My Documents\\", "sorted_list.txt"); byte[] charArray; try { reader = new BufferedReader(new FileReader(file)); reader1 = new BufferedReader(new FileReader(file)); String text = null; // repeat until all lines are read while ((text = reader.readLine()) != null) { contents.append(text).append(System.getProperty("line.separator")); count++; } list = new String[count]; count = 0; while ((temp1 = reader1.readLine()) != null) { list[count] = temp1; count++; } listlength = count; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } count = 0; int count1 = 0; String temp = null; String temp2 = null; String temp3 = null; for ( count = 0 ; count < listlength ; count++ ) { temp = list[count]; for ( count1 = 0 ; count1 < count ; count1++ ) { temp2 = list[count1].toLowerCase(); temp3 = list[count].toLowerCase(); if ( (temp2.indexOf(45) == -1) && (temp3.indexOf(45) == -1) ) { if ( temp2.compareTo(temp3) > 0) { temp = list[count]; list[count] = list[count1]; list[count1] = temp; count1++; } } else { if (temp2.indexOf(45) != -1) { temp2 = list[count1].substring(0, list[count1].indexOf(45)).toLowerCase(); //System.out.println(list[count]); } else { temp2 = list[count1].toLowerCase(); } if (temp3.indexOf(45) != -1) { temp3 = list[count].substring(0, list[count].indexOf(45)).toLowerCase(); } else { temp3 = list[count].toLowerCase(); } if ( temp2.compareTo(temp3) > 0) { temp = list[count]; list[count] = list[count1]; list[count1] = temp; count1++; } } } } String temp5; try { for (int i = 0 ; i < listlength ; i++) { //System.out.println(list[i]); charArray = list[i].getBytes(); temp5 = new String(charArray); //System.out.println(temp5); Files.write(file1, charArray); } } catch (IOException e) { System.out.println("IOException occurred"); } // show file contents here //System.out.println(contents.toString()); } }