writing and appending to a file

writing and appending to a file

My input file includes data from both males and females. My code below replaces a 'male string' with the key and values and writes into an existing file for 'male'. How can I do the same for females and write into a different 'female string' in the same program?

Sorry If I am confusing you!!! But I want something like

"going through my input----if it is a 'male', replace the 'male string' with corresponding key and values-------else if a 'female', replace the 'female string' with corresponding key and values".

public static void main(String[] args) {
Map<String, List<String>> MaleMap = new LinkedHashMap<String, List<String>>();
Map<String, List<String>> FemaleMap = new LinkedHashMap<String, List<String>>();
try {
        Scanner scanner = new Scanner(new FileReader(
                ":C/"));

        while (scanner.hasNextLine()) {
            String nextLine = scanner.nextLine();
            String[] column = nextLine.split(":");
            if (column[0].equals("Male") && (column.length == 4)) {
                MaleMap.put(column[1],
                        Arrays.asList(column[2], column[3]));
            } else if (column[0].equals("Female") && (column.length == 4)) {
                FemaleMap.put(column[1],
                        Arrays.asList(column[2], column[3]));
            }
        }
        Set<Entry<String, List<String>>> entries = MaleMap.entrySet();
        Iterator<Entry<String, List<String>>> entryIter = entries
                .iterator();
        while (entryIter.hasNext()) {
            Map.Entry entry = (Map.Entry) entryIter.next();
            Object key = entry.getKey(); // Get the key from the entry.

            List<String> value = (List<String>) entry.getValue();
            Object value1 = " ";
            Object value2 = " ";
            int counter = 0;
            for (Object listItem : (List) value) {
                Writer writer = null;
                Object Name = key;
                Object Age = null;
                Object ID = null;
                if (counter == 0) {// first pass assign value to value1
                    value1 = listItem;
                    counter++;// increment for next pass
                } else if (counter == 1) {// second pass assign value to value2
                    value2 = listItem;
                    counter++;// so we dont keep re-assigning listItem for further iterations
                }
            }
            System.out.println(key + ":" + value1 + "," + value2);
            scanner.close();
            Writer writer = null;
            Object Name = key;
            Object Age = value1;
            Object ID = value2;
            try {
                String filename = ".txt";
                FileWriter fw = new FileWriter(filename, true); // the true will append the new data
                fw.write("# Table" + Name + "\n" + "map:"
                        + Name + " a :ClassMap;" + "\n"
                        + " :dataStorage map:database;" + "\n"
                        + " :uriPattern " + Name + "/@@ "
                        + Age + "." + ID + "@@;" + "\n"
                        + " :class :" + Name);// appends the string to the file
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    } catch (Exception e) {
        e.printStackTrace();

    }
}
}
View Answers

Ads









Related Tutorials/Questions & Answers:
writing and appending to a file
writing and appending to a file  My input file includes data from... and values and writes into an existing file for 'male'. How can I do the same..." + " :class :" + Name);// appends the string to the file
Problem with appending data to the end of file
Problem with appending data to the end of file  MY JSP CODE... successfully</h1>"); %> </body> </html> JAVA CLASS FILE CODE... { public static void add(String name,String mail,String path) throws IOException { File
Advertisements
File saving and Writing
File saving and Writing   Hello Rose india..... I have a doubt... a "file create option" it will be ".csv" file and i want to write the data from arraylist into .csv file and i want to save that file so it will ask for "save
Writing and Reading A File
Writing and Reading A File  Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed... (FirstName, LastName, Age). I also made an empty file I named "contactsFile.txt" where
counting the values in input file and and writing the output to a file
counting the values in input file and and writing the output to a file  Can any one please correct this code. This is to read a file and later...-code/16483-counting-values-input-file-writing-output-file.html this code
Writing Log Records to a Log File
Writing Log Records to a Log File       This section demonstrates for writing log records to a log file. Logger provides different types of level like: warning, info
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written the following code to convert an ASCII text file to a binary file: public static... the binary file from another program as follows: m_dis = new DataInputStream
problem of writing to a local file ( JApplet ) - Applet
problem of writing to a local file ( JApplet )  Dear All, I want to program a guestbook using java applets but now I have problem of writing to a file from JApplet. it is working without any problem if i run the program using
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write... stumped when it comes to reading the file and writing to a new file. My code
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write... it comes to reading the file and writing to a new file. My code is as follows
without writing web.xml file we can write servlet program
without writing web.xml file we can write servlet program  Sir Morning... Sir I have one Question "without writing web.xml file we can write servlet program". if yes which way? if no why? but without use Annotation........ Plz
accessing data from a json file writing javascript or using jQuery
accessing data from a json file writing javascript or using jQuery  I... this huge json file containing several objects that contain arrays for the objects... page is built from this json file, but I can't figure out how to code
reading multiple files from a directory and writing them into a single file
reading multiple files from a directory and writing them into a single file... file i get an empty text file can you guide me how to do... static void main(String[]args) throws IOException{ File Folder = new
Example of appending to a String
Example of appending to a String  Hi, Provide me the example code of appending to a Java program. Thanks   Hi, Check the tutorial: How To Append String In Java? Thanks
Writing to File in Java
Writing to File in Java Writing any character or text in Java file is possible...) This is the simplest example of the class used for writing character file. BufferedWriter... are used for writing Character files and Text Files respectively while another class
Appending String efficiently in Java
Appending String efficiently in Java  Hi, Share me the example code for Appending String efficiently in Java. Thanks   Hi, StringBuilder class of Java API is used by developers of Java for appending string
Java File Writing Example
Java File Writing Example To write some data on the file you need to first open the file in append mode by using the java.io.FileWriter class and then pass... BufferedWriter(fileWriter); bufferedWriter.write("Adding New Information to the File
Appending Strings - Java Tutorials
.style1 { text-align: center; } Appending Strings In java, the two main ways for appending string at the end are : First method is using += operator. Second method is using append()  method. In this section, we
How to access the image file from project folder itself while writing to pdf using itext?
How to access the image file from project folder itself while writing to pdf using itext?  I am writing a pdf using itext where i add a image in the beginning of the document. When i use image = Image.getInstance("C
writing document (.doc) file from java with format such as font type, font style
writing document (.doc) file from java with format such as font type, font... with format such as font type and style to a document file? Thanks in advance... WriteInDocument{ public static void main(String[] args)throws IOException{ File file
writing characters
writing characters  how to write a group of characters to a string ... i mean for writing single character using charAt in java
Java Write To File Append
Java Write To File Append In this tutorial you will learn how to append the text when you are writing in a text file. Appending a text means that the text... the older text when you are writing again to an existing file. To achieve
Ghost Writing
Ghost Writing Ghost Writing is the kind of writing when someone... of writing it. So ghostwriter is a person who works behind the scene on a writing project in return of certain amount. The write-ups of the writer
Blog Writing
Blog Writing A blog, the short form of web log is an online user... on that. But the professionals are a better choice for writing the blog for commercial or promotional purpose. We provides the services of writing blogs for the commercial
Writing Actions
Writing Actions The Action is responsible for controlling of data flow within an application. You can make any java class as action. But struts have some... the method name in Action mapping in struts.xml file. To get model reference
dynamically writing text javascript
dynamically writing text javascript  dynamically writing text javascript. Is it possible
dynamically writing text javascript
dynamically writing text javascript  dynamically writing text javascript. Is it possible
Web Writing
Web Writing       The art of writing for the web is far removed from writing for the print world.... Following is the prime aspect you need to keep in mind while writing for the web
writing webservices in php
writing webservices in php  How to write a simple web service in PHP
SEO Writing
SEM and SEO Article Writing SEM and SEO article helps your business to grow... the intended traffic through SEM and SEO article writing in addition to other tools... is the demand of SEM and SEO article writing services. A good command over
Image writing over URLConnection
. The problem is that when writing by ImageIO on URL what will the name of the file...Image writing over URLConnection  Hello, I am trying to write... problems trying to write an image I have succeeded in writing to the local HD using
Writing and reading from/to a serialized file through Hash Table in Java
Writing and reading from/to a serialized file through Hash Table in Java... illustrates you how to read and write from/to a serialized file through the hash table.... Following program has the facility if the specified serialized file does not exist
Java Write GZIP File Example
Java Write GZIP File Example To add a file in GZIP format you need to use java.util.zip.GZIPOutputStream class. An example of writing a text file in GZIP...{ /* The new file name which you will get */ String newFileName
question "writing prog"
question "writing prog"  Hello I want helping for this question, please Write a program that reads some friendsā?? names, stores them in an array, and then prints out on the screen all friends who start by a particular letter
Reading And Writing Excel File
reading and writing excel file   ... org.apache.poi.poif.filesystem.POIFileSystem class is used to buffer the excel file into an object. In other words, we can say that it is used to read the excel file. After reading the excel
How to read file in java
How to read file in java Java provides IO package to perform reading and writing operations with a file. In this section you will learn how to read a text file line by line  in java. FileInputStream- This class reads bytes from
File
File  How to create .Exe file in jana
file
file  how can copy the information in the file to the string?   Store file data into String
Writing a trigger for an userdefined datatype
Writing a trigger for an userdefined datatype  i have a user defined datatype in EMP table like COLUMNNAME:ADDRS_TYPE where ADDRS_TYPE have the following columns DATATYPE :ADDRESS VARCHAR2(136) V_CODE VARCHAR2(8) T_CODE
file
file  how can add the information to the graphic file
file
file  Hi,what can delet some of the information of the file
file
file  Hi,how can delet some of the information of the file
File
File   How to find all files in our local computer using java
FILE
FILE  There is a file named Name.txt that contains information related to appliances.I want to read the contents of this file and I used the code... args[]) { try { File file=new File("Name.txt"); FileReader f=new FileReader(file
file
file  hi i want read special line of file. what am i doing ? I write... read data from file. please help me! void maketree(BtNode root,int i) throws... at the following link: Read specific line from file
writing programs - Java Beginners
writing programs  1.write a program to display odd numbers from 1-50. 2.And Write a program to find the facorial of a number  Hi Friend, 1) class OddNumbers{ public static void main (String args
freelance writing jobs
of writing content for web & print media.  To work in a team

Ads