How to read a specific value from a text file and then save the value in another text file.

Example:

in data.txt file there is some values.

Item Name : m-01

Item Brand : One Man

Item size : XXL

Item Price : 1000

Item vat : 15%
these are the data saved in data.txt

Now my program will ask for the item name and when I will write the item name(ex:m-01) It will just take the values 1000 ( price ) and 15 (vat) and then will save them in a new txt file data2.txt

How can I do this?

Help me please.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

ok lets again:

In my text file it cintain

@h Header
@074VH01MATT    TARA   A5119812073921 RONG HI  DE BET IA76200  201108222   0500  *
@074VH01KAYT    DJ     A5119812073921 RONG DED CR BET IA71200  201108222   0500  *
@f Footer

@h Header
@074VH01MATT    TARA   A5119812073921 RONG HI  DE BET IA76200  201108222   0500  *
@074VH01KAYT    DJ     A5119812073921 RONG DED CR BET IA71200  201108222   0500  *
@f Footer

My code is here to read some string:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class StringData {
    public static void main(String[] args) throws Exception {
        BufferedReader br = null;
        try {
            // change this value
            FileInputStream fis = new FileInputStream("data.txt");
            br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                processLine(sCurrentLine);
            }
        } finally {
            if (br != null) br.close();
        }
    }

    public static void processLine(String line) {
        // skip header & footer
        if (line.startsWith("@h Header") || line.startsWith("@f Footer")) return;

        String name = line.substring(8, 20);
        String year = line.substring(63, 67);

        System.out.println("Name [" + name + "]\t Year [" + year +"]");
    }
}

now how to save readed data to a another text file named data2.txt?

You have your data on ines 25,26 and you have an example of how to write those to an output stream on line 28. The output file stuff is a mirror image of the input file code you already have, so:

create a FileOutputStream for data2.text
create a PrintWriter using that FileOutputStream
print your data to the PrintWriter
close the output file

Check out the API documentation for FileOutputStream and PrintWriter, and look at the "line oriented IO" section here

Hy tnx...i have done it

FileWriter f_m_all = new FileWriter("data2.doc");
PrintWriter out_m_all = new PrintWriter(f_m_all);

out_m_all.print("\nName           : "+name);
out_m_all.print("\nYear           : "+year);

out_m_all.close();
out_m_all.close();

now i have a little problem....

i m collecting strings from the text file.
if the strings represents just set of numbers (num1: 500 , num2: 600) then how can i convert them to integer/double/floating type so that i can do some arithmatical solution with them.

Example: 500+600

Oh ya i have done it :p

System.out.println(Double.parseDouble(num1)+Double.parseDouble(num2));

he he he

The Integer class has a parseInt method to convert a String to an Integer, similarly for Double etc

edit: (Too late - you already got it)

i have one more qus.

How can i save data into text file to a fixed position ?

I need to konw it because i am reading some string from a fxied position of a text file.
So i need to save the data into the text file to a fixed position from which i can read the data whenever i need. thnx :p

Do you mean like overwriting one line in the file or something like that?
Easiest way is to read the file into a String[] then update it, then write it all out again.
Or do you mean in fixed format, in which case printf is probably your best freiend

No no. I just want to save data to a fixed position of a text file. So i can easyly read them from the fixed position .

Ex:
In data.txt i wanna save 500 in position (1, 4)

Because i want to read them from from position (1, 4)

Thats mean the Write Into file program will save 500 in between (1, 4) position into the text file and Read program will take the value(500) from (1, 4) position from the text file

OK. printf is like print or println but lets you specify format info for the data you are printing, such as field width, no of decimal places etc. There's lots of examples and syntax on the web.
(I'm finishing now until tomrrow)

:( . i will wait for your next reply if i unable to do the program... thnx for your help @James

Thankx Problem Solved.

It Was easy task what i asked at last time.

I have solved it with another way.

When writing into a file then i just printed some extra space and aftar that When reading from the file ,just read the string with extra space. In this way i can easily write pre-defined leangth and then can read them easily.

My both codes are here :P

Writing into text file:(keybord input)

FileWriter f_m_values = new FileWriter("txt.doc");
PrintWriter out_m_values = new PrintWriter(f_m_values);

out_m_values.println("@HEADER");
out_m_values.print(price_m+"            "+discount_m+"            "+vat_m+"            *\n");
out_m_values.print("@FOOTER");

out_m_values.close();
f_m_values.close();

Reading from the text file:(Same Code in reply no:3)
**Same Code in reply no:3:
Just replace the processLine() method

**

public static void processLine(String line)
{
    // skip header & footer
    if (line.startsWith("@HEADER") || line.startsWith("@FOOTER")) return;


    String Amount = line.substring(0,12);
    String Dis = line.substring(13, 24);
    String Vat = line.substring(25, 35);
    System.out.println("Amount [" + Amount + "]\t Dis [" + Dis +"]\t Vat [" + Vat +"]\n");

    String t_a=((Double.parseDouble(Amount)+ ((Double.parseDouble(Amount)*Double.parseDouble(Vat))/100))-((Double.parseDouble(Amount)*Double.parseDouble(Dis))/100)+"\n");

    System.out.print("Total Amount : "+Double.parseDouble(t_a)+"\n\n");

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.