| | |
Writing to a file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception file fractal free game gameprogramming gis givemetehcodez graphics gui helpwithhomework html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






