java - reading/writing variables from text files to variables -
i need make system storing customer information , quotations external file entering more customers, listing customers, , same quotations. need link quotations/customers id. need sql in java. however, need input , output system, , writing info array. have got 2 main pieces of code inefficient , need suggestions, improvements or entirely different system. input file code:
import java.io.*; //import classes import java.util.arraylist; import java.util.iterator; public class mytextreader{ public static void main(string[] args){ string mydirectory = system.getproperty("user.dir"); string fulldirectory = mydirectory + "\\mytext.txt"; string input_line = null; arraylist<string> textitems = new arraylist<string>(); //create array list try{ bufferedreader re = new bufferedreader(new filereader(fulldirectory)); while((input_line = re.readline()) != null){ textitems.add(input_line); //add item array list } }catch(exception ex){ system.out.println("error: " + ex); } iterator myiteration = textitems.iterator(); //use iterator cycle list while(myiteration.hasnext()){ //while items exist system.out.println(myiteration.next()); //print item command-line } } }
output file
import java.io.filewriter; //import classes import java.io.printwriter; public class mytextwriter{ public static void main(string[] args){ filewriter writeobj; //declare variables (uninstantiated) printwriter printobj; string mytext = "hello text file"; try{ //risky behaviour – catch errors writeobj = new filewriter("c:\\documents\\mytext.txt" , true); printobj = new printwriter(writeobj);//create both objects printobj.println(mytext); //print file printobj.close(); //close stream }catch(exception ex){ system.out.println("error: " + ex); } } }
for reading text file
filereader fr = new filereader("yourfile.txt"); bufferedreader br = new bufferedreader(fr); string s=""; s=br.readline(); system.out.println(s);
for writting text file
printwriter writetext = new printwriter("yourfile.txt", "utf-8"); writetext.println("the first line"); writetext.println("the second line"); writetext.close();
Comments
Post a Comment