Results 1 to 9 of 9
Thread: Input in a txt file
- 05-11-2011, 05:23 PM #1
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Input in a txt file
Hello i want to write in a txt file name "Dogs.txt" i have done this code but in my file i can't see my inputs!i see null!! any helps???
Java Code:public class Items extends Pelatis{ public String Id; public int Posotita; public String Perigrafi; public double Price; public void setId(String val){ this.Id=val; } public String getId(){ return Id; } public void setPosotita(int val){ this.Posotita=val; } public int getPosotita(){ return Posotita; } public void setPerigrafi(String val){ this.Perigrafi=val; } public String getPerigrafi(){ return Perigrafi; } public void setPrice(double val){ this.Price=val; } public double getPrice(){ return Price; } //me8odos morfopoihmenhs epistrofhs se String twn metavlhtwn ths klashs(stoixeiwn ma8hth) public String showDetails() { return ("Κωδικός : "+getId()+ "\n Διαθέσιμη Ποσότητα:"+ getPosotita()+ "\nΠεριγραφή: " + getPerigrafi() + "\nΤιμή: " + getPrice()); } public int showChangeDetails() { String msg = String.format("Θέλετε να αλλάξετε την παρακάτω εγγραφή;\n" + showDetails()); int yes_or_no = JOptionPane.showConfirmDialog(null, msg, "modify student", JOptionPane.YES_NO_OPTION); return yes_or_no; } //me8odos perasmatos timwn stis metavlhtes ths Items public void addStoixeia() { int numberformat = 0; int numberdigits = 0; // read in user input JTextField id = new JTextField(); JTextField posotita = new JTextField(); JTextField price = new JTextField(); JTextField perigrafi = new JTextField(); final JComponent[] inputs = new JComponent[]{ new JLabel("Κωδικός"), id, new JLabel("Διαθέσιμη Ποσότητα"), posotita, new JLabel("Τιμή"), price, new JLabel("Περιγραφή"), perigrafi }; //elegxos pws kapoio apo ta plaisia den einai kena while (id.getText().equals("") || posotita.getText().equals("") || price.getText().equals("") || perigrafi.getText().equals("") || numberformat == 1 || numberdigits == 1) { try{ JOptionPane.showMessageDialog(null, inputs, "ΚΑΤΑΧΩΡΗΣΗ", JOptionPane.PLAIN_MESSAGE); Id = id.getText(); Posotita = Integer.parseInt(posotita.getText()); Price = Double.parseDouble(price.getText()); Perigrafi = perigrafi.getText(); } catch(NumberFormatException nfe) { //Handle exception numberformat = 0; } if (id.getText().equals("") || posotita.getText().equals("") || price.getText().equals("") || perigrafi.getText().equals("") || numberformat == 1) { JOptionPane.showMessageDialog(null, "Θα πρέπει να συμπληρώσετε όλα τα πεδία!", "Απέτυχε", JOptionPane.WARNING_MESSAGE); } } } //synarthsh evreshs tou id sto arxeio public String[] findFullName(String filename, String Id) { String[] grammes = {"$%^&*", null, null, null}; int x = 0; try { boolean check = false; // Open the file that is the first command line parameter FileInputStream fstream = new FileInputStream(filename); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { //append(save) se arxeio txt String[] temp = null; //h sunarthsh delimeter spaei to string se 3 kommatia dinontas times ston pinaka temp[] me 3 theseis //se onoma, epwnumo kai se ena trito kommati (dieu8unsh#thlefwno mazi) pou omws de mas afora edw temp = lineDelimiter(strLine, 4, "#"); //elegxei an to onomatepwnumo pou dwsame taytizetai me to onomatepwnumo ths ekastote grammhs //an nai tote oloklhrh h grammh pernietai ston pinaka grammes[] if (temp[0].equals(Id)) { check = true; grammes[x] = strLine; x = x + 1; } } //close input stream in.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } return grammes; } //epistrefei th String anaparastash twn metavlhtwn ths Studentz me thn pros8hkh twn diaxwristikwn "#" //wste na mporei na ginei metepeitai o diaxwrismos apo thn delimeter (xrhsimeyei sth apo8hkeysh se txt) public String toString() { return String.format(getId() + "#" + getPosotita() + "#" + getPrice() + "#" + getPerigrafi()); } //pros8hkh(append) mias grammhs String se arxeio .txt me onoma filename public void write2Txt(String temp2, String filename) { try { boolean append = true; FileWriter fw = new FileWriter(filename, append); fw.write(temp2 + "\n");//appends the string to the file fw.close(); } catch (IOException ioe) { String message1 = String.format(ioe.getMessage()); JOptionPane.showMessageDialog(null, message1, "file save error", JOptionPane.WARNING_MESSAGE); } } //tropopoihsh stoixeiwn mias grammhs enos arxxeiou filename pou periexei to zhtoumeno Id public void modifyItem(String filename, String Id) { try { boolean check = false; String tempid = null;//gia perasma sthn findname int change = 1; // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(filename); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { //append(save) se arxeio txt // Print the content on the console String[] temp = null; temp = lineDelimiter(strLine, 4, "#"); // se posa strings tha spasei px 4 edw if (temp[0].equals(Id)) { check = true; Items itm = new Items(); itm.setId(temp[0]); itm.Posotita = Integer.parseInt(temp[1]); itm.Price = Double.parseDouble(temp[3]); itm.setPerigrafi(temp[2]); change = itm.showChangeDetails(); if (change == 0) { itm.addStoixeia(); strLine = itm.toString(); //pairnei to Id wste parakatw na elegksei an sto temp uparxei diplo-eggrafh tempid = itm.getId(); } } Items eggrafhstoarxeio = new Items(); eggrafhstoarxeio.write2Txt(strLine, "temp.txt"); //dhmiourgia tou temp.txt me allagmenh thn mia grammh } //Close the input stream in.close(); String result3[] = findFullName("temp.txt", tempid); if (result3[1] != null)//an dld uparxoun 2 akrivws idies egrafes { String msg = String.format("Το προϊόν με κωδικο :"+" "+tempid+" "+" υπάρχει ήδη η ποσότητα άλλαξε"+" "+result3[1]); JOptionPane.showMessageDialog(null, msg, "Ο ΧΡΗΣΤΗΣ ΥΠΑΡΧΕΙ ΗΔΗ!", JOptionPane.ERROR_MESSAGE); File oldbackup = new File("temp.txt"); oldbackup.delete(); } } catch (Exception e) {//Catch exception if any //System.err.println("Error: " + e.getMessage()); String message1 = String.format(e.getMessage()); JOptionPane.showMessageDialog(null, message1, "file not found", JOptionPane.WARNING_MESSAGE); } } public String[] lineDelimiter(String str, int num, String symboldelimit) { //elegxetai posa stoixeia ehei to string analoga me to noumero num String[] tempstr = new String[num]; /* delimiter-sumvolo diaxwrisths */ String delimiter = symboldelimit; // se posa strings tha spasei px "num" edw tempstr = str.split(delimiter, num); //epistrefei ton pinaka me ta epimerous stoixeia (pinakas mege8ous num) return tempstr; } }
Java Code:if(inputs=="ΚΑΤΑΧΩΡΗΣ"){ Items itm=new Items(); itm.addStoixeia(); String result2[] = itm.findFullName("Dogs.txt", itm.getId()); if (result2[0] == "$%^&*") { itm.write2Txt(itm.toString(), "Dogs.txt"); String informationmessage2 = String.format("Η εγγραφή σας ολοκληρώθηκε με επιτυχία"); JOptionPane.showMessageDialog(null, informationmessage2,"ΟΛΟΚΛΗΡΩΣΗ ΕΓΓΡΑΦΗΣ",JOptionPane.INFORMATION_MESSAGE); break; } else { dog.write2Txt(dog.toString(), "Dogs.txt"); String informationmessage4 = String.format("Η καταχώρησή σας υπαρχει "); JOptionPane.showMessageDialog(null, informationmessage4,"ΟΛΟΚΛΗΡΩΣΗ ΚΑΤΑΧΩΡΗΣΗΣ",JOptionPane.INFORMATION_MESSAGE); break; } }
- 05-13-2011, 12:29 AM #2i see null!
- 05-13-2011, 01:08 AM #3
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
thanks for your answer!!i have solve it!!!!now i have another probleb!!
I want to update an input i have done for example when i give an Id that is allready in my file i want to update Posotita(new + old )
here in my code i found the sting line i want to change, but i can update it whith my new data (newPosotita) Any helps??
if(inputs=="ΚΑΤΑΧΩΡΗΣΗ "){
Dogs dog=new Dogs();
dog.addStoixeia();
int tempPosotita=dog.getPosotita();//i posotita pou dinw
String result2[] = dog.findFullName("Dogss.txt", dog.getId());
if (result2[0] == "dog.getId()"){
String temp[] = null;
temp= dog.lineDelimiter(result2[0], 4, "#");
dog.setId(temp[0]);
dog.Posotita=Integer.parseInt(temp[1]);
dog.Price=Double.parseDouble(temp[2]);
dog.setPerigrafi(temp[3]);
temp[0] = dog.toString();
int tempoldPosotita =dog.getPosotita();
int newPosotita=tempoldPosotita+tempPosotita;//i allagmeni posotita
dog.setPosotita(newPosotita);
Dogs eggrafhstoarxeio = new Dogs();
eggrafhstoarxeio.write2Txt(temp[0], "temp.txt");
rename("temp.txt", "Dogss.txt");
String msg =
String.format("Η καταχώρησή σας ενημερώθηκε με επιτυχία");
JOptionPane.showMessageDialog(null, msg, "ΚΑΤΑΧΩΡΗΣΗ", JOptionPane.INFORMATION_MESSAGE);
}
- 05-13-2011, 01:16 AM #4
Sorry, I don't understand your problem. Could you explain or show an example?
It sounds like a substring problem. Look at the String class. It has methods for replacing parts of a String with another String.
- 05-13-2011, 02:16 PM #5
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
I wan't to update an input in a txt file! for example i have this data in a txt file (Id=Dog,Posotita=1,Price=100,Perigrafi=good) and give thise data as an input(Id=Dog,Posotita=2,Price=100,Perigrafi=good) I want if Id is the same to update this line in my file like this Id=Dog,Posotita=3(1+2),Price=100,Summary=good
- 05-13-2011, 03:45 PM #6
Did you look at the String class? It has methods for replacing parts of a String.
- 05-13-2011, 07:21 PM #7
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
i used a replace mathod and it works! But i want to replace the old line with the new in my file! Now i get the newline and the old!
Java Code:String temp[] = null; temp= dog.lineDelimiter(result2[0], 4, "#"); dog.setId(temp[0]); dog.Posotita=Integer.parseInt(temp[1]); dog.Price=Double.parseDouble(temp[2]); dog.setPerigrafi(temp[3]); temp[0] = dog.toString(); int tempoldPosotita =dog.getPosotita(); int newPosotita=tempoldPosotita+tempPosotita; dog.setPosotita(newPosotita); String oldPosotita = Integer.toString(tempoldPosotita); String newfPosotita = Integer.toString(newPosotita); String newline=temp[0].replace(oldPosotita,newfPosotita); Dogs eggrafhstoarxeio = new Dogs(); eggrafhstoarxeio.write2Txt(newline, "Dogss.txt");
- 05-13-2011, 10:59 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 64
- Rep Power
- 0
In the write2Txt method you are appending to the file:
Java Code:FileWriter fw = new FileWriter(filename, append);
You should do what you did in the method
Java Code:public String[] findFullName(String filename, String Id) {....
if the Id matches in a line, do the replacement in that line,
and save all the lines back into the file.
- 05-14-2011, 12:46 AM #9
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
you mean sth like this??
the newvalue is the value that i take as an input i want to pass it through main if i can!!!
Java Code:public void updateItem(String filename,String Id,int newvalue){ //open the file,read line by line and find correct line String[] grammes = {"$%^&*", null, null, null}; int x = 0; try { boolean check = false; // Open the file that is the first command line parameter FileInputStream fstream = new FileInputStream(filename); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { //append(save) se arxeio txt String[] temp = null; //h sunarthsh delimeter spaei to string se 3 kommatia dinontas times ston pinaka temp[] me 3 theseis temp = lineDelimiter(strLine, 4, "#"); //elegxei an to ID pou dwsame taytizetai me to ID ths ekastote grammhs //an nai tote oloklhrh h grammh pernietai ston pinaka grammes[] if (temp[0].equals(Id)) { check = true; grammes[x] = strLine; x = x + 1; Items itm = new Items(); String newtemp[] = null; newtemp= lineDelimiter(grammes[x], 4, "#"); itm.setId(newtemp[0]); itm.Posotita=Integer.parseInt(newtemp[1]); itm.Price=Double.parseDouble(newtemp[2]); itm.setPerigrafi(newtemp[3]); grammes[x] = itm.toString(); int tempoldPosotita =itm.Posotita; int newPosotita=tempoldPosotita+newvalue;//i allagmeni posotita String oldPosotita = Integer.toString(tempoldPosotita); String newfPosotita = Integer.toString(newPosotita); grammes[x]=newtemp[0].replace(oldPosotita,newfPosotita); itm.write2Txt(grammes[x], filename); } } //close input stream in.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } }
and this in main
itm.updateItem("Dogss.txt",itm.getId(),itm.getPoso tita());Last edited by AARON22; 05-14-2011 at 12:55 AM. Reason: fdf
Similar Threads
-
Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02-10-2011, 10:55 AM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 08:19 PM -
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 05:01 PM -
input from file
By freddyvorhees in forum New To JavaReplies: 5Last Post: 10-01-2009, 06:37 PM -
retain value of input type file in a jsp file while being dynamically generated
By nidhi c in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2009, 03:21 AM
Bookmarks