import java.io.*; // save existing file buffer.save(view,null,true); //... Create File objects. File outFile = new File("d:/data/marked.do"); // write into second file //... Enclose in try..catch because of possible io exceptions. try { //... Create reader and writer for text files. BufferedWriter writer = new BufferedWriter(new FileWriter(outFile)); Selection[] selection = textArea.getSelection(); if(selection == null) view.getToolkit().beep(); else { for(i = 0; i < selection.length; i++) writer.write(textArea.getSelectedText(selection[i])); writer.newLine(); // Write system dependent end of line. //... Close reader and writer. writer.close(); // Close to unlock and flush to disk. } } catch (IOException e) { System.err.println(e); System.exit(1); } // clear selection textArea.goToPrevLine(false); textArea.goToNextLine(false);