+ 1
What does this code?
I played around with the code below. The game was "create another error". Still no clue how to use it. And I need to use it. Some hints maybe?
18 Answers
+ 18
As far as I can tell, it reads in the text from input.txt one line at a time and then writes that line to output.txt.
+ 18
I think it should be writer.println(cardDetails)
Also, leave your comments so others can see how you ended up solving your problem 🍰
+ 18
Dunno about Java but in C#, if you try to write to a file that doesn't exist, it creates one for you and then writes to it
+ 18
Just Compilation error. I thought code playground would be able to compile it, at least. If the file was wrong, wouldn't there be a runtime error?
+ 17
@Ace I think he meant his "answer" to this post 😀
+ 17
There's a share button in the top right of code playground
+ 17
I'm getting Compilation error
+ 17
I'll just take your word for it that it works 😄
+ 16
Make sure you're using writer.println(cardDetails), writer.flush() and writer.close()
+ 3
So basically if I need to record into a file what a user types, I just need this part of code aren't I?
FileOutputStream fos = new FileOutputStream("output.txt");
PrintWriter writer = new PrintWriter(fos);
and
System.out.println("Tell me your debit card number and the pin. Just for fun.");
Scanner scanner2 = new Scanner(System.in);
long cardDetails = scanner2.nextLong();
writer.println(cardDetails);
I'll try to do it. If this comment stays, the try was unsuccessful.
+ 3
The good news is, it compiled. What I am worried about is how does the program should access output.txt? Even java compiler needs exact directory. No attempts to record sth into the file via the program were successful. It doesn't even mind that I do so:
FileOutputStream fos = new FileOutputStream("HolyBible.txt");
It compiled despite the fact that I am an atheist nor there doesn't exist such a file on my PC.
+ 2
FileWriter.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileWriter {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("input.txt"));
FileOutputStream fos = new FileOutputStream("output.txt");
PrintWriter writer = new PrintWriter(fos);
while (scanner.hasNext()) {
String line = scanner.nextLine();
writer.println(line);
}
writer.flush();
writer.close();
}
}
+ 2
A little improvement. The file is automatically created and the problem is in writer() function, i think
+ 2
Yup. Done. Thanks for great help!
---
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileWriter {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream("output.txt");
PrintWriter writer = new PrintWriter(fos);
System.out.println("Say something ");
Scanner scanner = new Scanner(System.in);
String sth = scanner.nextLine();
writer.println(sth);
writer.flush();
writer.close();
}
}
+ 2
How do I copy the link in the app?
+ 2
https://code.sololearn.com/cMO9TgASSJUw/?ref=app
You can't see the file so it is useless in the app.
+ 2
@Jafca
Give it a try in a special IDE
+ 2
Anyways, you know where to find it if needed. Thanks for the help. Good night, morning or evening.