00001 package TxTPackage;
00002 import java.io.*;
00003 import java.util.ArrayList;
00004
00005
00006 public class FileSchrijver{
00007
00008
00009
00010
00011
00012 private BufferedWriter writer;
00013 private String pathtofile;
00014
00015
00016 private static String writeDirectory=getCurrentDirectory();
00017 public FileSchrijver(String filename_) {
00018
00019
00020 this.pathtofile=writeDirectory+"/"+filename_;
00021
00022 try{
00023 writer = new BufferedWriter(new FileWriter(this.pathtofile));
00024 }
00025 catch(Exception e) {
00026 e.printStackTrace();
00027 printErrorMessage();
00028 }
00029
00030 }
00031
00032 public String getAbsoluteFileName(){
00033 return this.pathtofile;
00034 }
00035
00036 public static String getCurrentDirectory(){
00037 File f=new File(".");
00038 String currentdir=null;
00039 try{currentdir=f.getCanonicalPath();}
00040 catch(Exception e){
00041 Debug.print("could not find current directory");
00042 }
00043 return currentdir;
00044 }
00045
00046 public static void setWriteDirectory(String nieuwe_writeDirectory){
00047 writeDirectory=nieuwe_writeDirectory;
00048 }
00049
00050 public static String getWriteDirectory(){
00051
00052 return writeDirectory;
00053 }
00054
00055 public void flush(){
00056 try{
00057 writer.flush();
00058 }
00059 catch(Exception e) {
00060 e.printStackTrace();
00061 }
00062 }
00063
00064 public void sluit(){
00065 try{
00066 writer.flush();
00067 writer.close();
00068 }
00069 catch(Exception e) {
00070 e.printStackTrace();
00071 }
00072 }
00073 public void schrijf(float getal){
00074 try{
00075 writer.write(""+getal);
00076 }
00077 catch(Exception e) {
00078 e.printStackTrace();
00079 printErrorMessage();
00080 }
00081 }
00082
00083
00084 public void schrijf(ArrayList<String> codewoorden){
00085 for(int i =0;i!=codewoorden.size();i++){
00086 try{
00087 writer.write(codewoorden.get(i));
00088 this.nieuweLijn();
00089 }
00090 catch(Exception e) {
00091 e.printStackTrace();
00092 printErrorMessage();
00093 }
00094 }
00095
00096 }
00097
00098 public void schrijfAanElkaar(ArrayList<String> codewoorden){
00099 for(int i =0;i!=codewoorden.size();i++){
00100 try{
00101 writer.write(codewoorden.get(i));
00102 }
00103 catch(Exception e) {
00104 e.printStackTrace();
00105 printErrorMessage();
00106 }
00107 }
00108
00109 }
00110
00111 public void schrijfMetSpaties(ArrayList<String> codewoorden){
00112 for(int i =0;i!=codewoorden.size();i++){
00113 try{
00114 writer.write(codewoorden.get(i));
00115 writer.write(" ");
00116 }
00117 catch(Exception e) {
00118 e.printStackTrace();
00119 printErrorMessage();
00120 }
00121 }
00122
00123 }
00124
00125 public void schrijfMetRegels(ArrayList<String> codewoorden){
00126 for(int i =0;i!=codewoorden.size();i++){
00127 try{
00128 writer.write(codewoorden.get(i));
00129 this.nieuweLijn();
00130 }
00131 catch(Exception e) {
00132 e.printStackTrace();
00133 printErrorMessage();
00134 }
00135 }
00136
00137 }
00138
00139
00140 private void printErrorMessage(){
00141 Debug.print("Kan niet schrijven naar "+ pathtofile);
00142 Debug.print("Mss is er een ander programma dat dit in gebruik heeft, kies dan een andere naam of sluit die toepassing af.");
00143 Debug.print("filename= "+ this.pathtofile);
00144 }
00145 public void nieuweLijn(){
00146 try{
00147 writer.newLine();
00148 }
00149 catch(Exception e) {
00150 e.printStackTrace();
00151 printErrorMessage();
00152 }
00153 }
00154 public void schrijf(String woord){
00155 try{
00156 writer.write(woord);
00157 }
00158 catch(Exception e) {
00159 e.printStackTrace();
00160 printErrorMessage();
00161 }
00162 }
00163
00164
00165
00166 public static void main(String[] args) {
00167 FileSchrijver fileSchijver1 = new FileSchrijver("test.txt");
00168 fileSchijver1.schrijf(0.4567f);
00169 fileSchijver1.schrijf(56.432f);
00170 }
00171 }