| | |
Writing to a file
![]() |
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
•
•
•
•
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.util.StringTokenizer;
public class Sample {
public static int count = 0;
public static String s[]=new String[2];
public static String s2;
public static String s3;
public static void main (String args[]) throws IOException{
isRead();
}
public static void isRead()throws IOException{
int count2 = 0;
BufferedReader lnr = new BufferedReader (new FileReader("subjects.txt"));
String line;
String ask;
while ((line = lnr.readLine())!=null){
StringTokenizer st = new StringTokenizer(line);
s[count2] = st.nextToken();
count2++;
}
isInput();
}
public static void isInput() throws IOException{
FileWriter writer = new FileWriter("output.txt");
int i;
String ask = JOptionPane.showInputDialog("Enter.");
if(ask.equals(s[0])){
StringTokenizer st = new StringTokenizer(ask,s[0],true);
while(st.hasMoreTokens()){
String ask2 = st.nextToken();
if(ask2.equals("1")){
i = Integer.parseInt(ask2);
}
else if(ask2.equals("M")){
s2 = "Math17";
}
}
String semester = "\n1\n";
writer.write(semester+s2);
writer.flush();
writer.close();
isInput();
}
else if(ask.equals(s[1])){
StringTokenizer st = new StringTokenizer(ask,s[1],true);
while(st.hasMoreTokens()){
String ask2 = st.nextToken();
if(ask2.equals("2")){
i = Integer.parseInt(ask2);
}
else if(ask2.equals("I")){
s3 = "IT110";
}
}
String semester = "\n1\n";
writer.write(semester+s3);
writer.flush();
writer.close();
}
}
(1,Math17)
(2,IT110)
Please help me on how to fix this. It only writes the last one, which is IT110, and no Math17 appears. Was the first one that i write was saved? Or everytime i write a new one, the old one will be deleted..I hope you can help me. Thank you.
You need to explain what you are trying to accomplish and how because your code doesn't make any sense
Check out my New Bike at my Public Profile at the "About Me" tab
Do you have something personal against the code tags that after our repeated attempts to get you start using them, you still don't.
As far as the programs goes, my comments could be copy-pasted from those of javaAddict.
As far as the programs goes, my comments could be copy-pasted from those of javaAddict.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
![]() |
Similar Threads
- writing to file (Python)
- writing to a file (C++)
- Writing in a file (C++)
- Read/write to same file > once, Help (C++)
- writing to a file (Java)
- Saving a file (Java)
- file input and output (C)
Other Threads in the Java Forum
- Previous Thread: setUndecorated/Dragging
- Next Thread: Need help with Caesar Cipher program
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






