Bret Dun Bret Dun - 1 month ago 6
Java Question

I need to read a .txt file from a java and write the sorted context to a different .txt file

I have a I/O java file, a SDive file which contains main, a .txt Directory file that has random words in it and a Sorted .txt to return the random words in order ascending to descending. It print all the words in the Sorted but it is not sorted.

//Sort.java
// required for input
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
// required for output
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

class Sort{

private String[] tArr = new String[100];

public void swap(int j) {

String temp = tArr[j-1];
tArr[j-1] = tArr[j];
tArr[j] = temp;
}

///Bubble sort
public void sort() {
int n = tArr.length;
for(int i = 0; i < n; i++) {
for(int j = 1; j < n-i; j++) {
if(tArr[j-1].compareTo(tArr[j]) > 0) {
swap(j);
}
}
}
}

public void read() {

System.out.println("in read()");
String pwd = System.getProperty("user.dir");
String fileName = pwd + "/directory.txt";
System.out.println("Looking for: " + fileName);
String fileLine = "";

try {

File f = new File(fileName);

if(f.exists()) {
System.out.println("Directory profile found, loading data...");
}
else {
System.out.println("Directory profile not found, loading default...");

return; // done, return back to the caller
}

// Read file
FileReader data = new FileReader(fileName);

// Wrap FileReader with BufferedReader
BufferedReader br = new BufferedReader(data);
//String tmp;

int i=0;
while ((fileLine = br.readLine()) != null) {
tArr[i++] = fileLine;
}

// ok, time to load an existing profile


// close the file
br.close();

} catch (FileNotFoundException fnf) {
System.out.println("File not found: " + fileName);
} catch (IOException ioe) {
System.out.println("Error reading file: " + fileName);
} catch (Exception exc) {
System.out.println("Something went wrong: " + fileName);
}

}

public void write() {

System.out.println("in write()");
String pwd = System.getProperty("user.dir");
String fileName = pwd + "/Sorted.txt";

try {

System.out.println("Writing out to: " + fileName);

File file = new File(fileName);
// creates the file
file.createNewFile();
// create FileWriter object
FileWriter writer = new FileWriter(file);

// output to file
// ADD pIdx, pArr, mood, and anything else here...

for(int i = 0; i < tArr.length; i++) {
writer.write(tArr[i] + "\n");
}

writer.flush();
writer.close();

} catch (IOException ex) {
System.out.println("Error writing to file: " + fileName);
}

}

}


And my main file is SDrive:

class SDriver{

public static void main(String args []){
Sort io = new Sort();
io.read();
io.write();

}
}

Answer

You should add the io.sort() method after io.read(); line and just before io.write(); line.

I see you use bubble sort, if you really want to implement your own sorting method take a look at quick sort and merge sort which are much much faster than bubble sort on larger arrays but also harder to implement. Insertion and Selection sort are not as fast as merge or quick but still faster than bubble and still as easy to self implement. Or use Arrays.sort(tArr); if you want to do it quick.