I have 3 text files. Numbers.txt have int and double values like 1 1.5 2... I wanna put my int values to my Int.txt and double values to Double.txt. So how can I do?
I tried .hasNextDouble() or .hasNextInt()
public static void main(String[] args) {
File f = new File("a.rtf");
File fWrite = new File("aWrite");
try {
FileReader fr = new FileReader(f);
FileWriter fw = new FileWriter(fWrite);
double c = fr.read();
while(c != -1){
char k = (char)c;
c.hasNextDouble();
System.out.print(k + " ");
fw.write((int) c);
c = fr.read();
}
fr.close();
fw.close();
} catch(Exception e) {
e.printStackTrace();
}
I am new in Java. What else can I try?
You can try this:
Writer wr = new FileWriter("aWrite.txt");
wr.write(String.valueOf(1));
wr.write(String.valueOf(1.5));
wr.write(String.valueOf(2));
wr.flush();
wr.close();
Or this approach which is mainly better for bigger data:
File file = new File("aWrite.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("Write the string to text file");
out.newLine();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With