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
Related Tutorials/Questions & Answers:
writing and appending to a filewriting 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 fileProblem 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 FileWriting 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
File Writing - Java BeginnersFile Writing Hi... I need a syntax or logic or program by which we can write into the desired shell of the Excel
file from console window... Hi friend,
I am sending you a link. This is will help you. Please
Writing xml file - Java BeginnersWriting xml file Thank you for the quick response
The values which... XmlServlet().createXmlTree(doc);
System.out.println("Xml
File Created... = sw.toString();
File file = new
File("c:/newxml.xml
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 file using servlets - JSP-ServletWriting a
file using servlets I'm using a servlet to read an input... reading the data and creating a
file in my space. but it is not
writing..., but its not
writing in the xyz.txt
file. I think
writing in a servlet should
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
java program for writing xml file - Java Beginnersjava program for
writing xml file Good morning
i want to write values from my database(one table)into one xml
file.
Like i have 3 coloumns in my... xml
file and storet that in particlar location.
Please help me out
Thanks
problem of writing to a local file ( JApplet ) - Appletproblem 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
Example of appending to a StringExample 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
Appending String efficiently in JavaAppending 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
Writing to File in JavaWriting to
File in Java
Writing any character or text in Java
file is possible...
file)
This is the simplest example of the class used for
writing character
file... are used for
writing Character files and Text Files respectively while another class
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
Java File Writing ExampleJava
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 writing characterswriting 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 AppendJava 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 WritingBlog
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 ActionsWriting 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
writing plugin - WebSeviceswriting plugin can anyone help me in
writing plugin for internet explorer browser using c++ in .net environment
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
SEO WritingSEM 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
Reading and Writing files - Java BeginnersReading and
Writing files Hello,
please help me to Develop a simple... from a
file. After welcoming the users, ask them for their name and save it to the
file. Use the ClassLoader and Properties classes to load the
file. You will need
Java Write GZIP File ExampleJava 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
Writing RDF file in Java
Writing RDF
file in Java
... or Resource Description Framework
file in
Java. The example illustrates you how... and example code to develop RDF
file in
Java
"createDefaultModel()"
writing aprogram - Java Beginnerswriting aprogram Write a program to reverse any string Hi Friend,
Try the following code:
import java.util.*;
class StringReverse{
public static void main(String[]args){
Scanner input=new Scanner(System.in