Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created June 2, 2020 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariiaKolokolova/69616ae128221a44913c0acc02b8c233 to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/69616ae128221a44913c0acc02b8c233 to your computer and use it in GitHub Desktop.
package maricka.kolokolova;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("b.txt");
try {
file.createNewFile();
} catch (IOException e) {
System.out.println(e);
}
Scanner sc = new Scanner(System.in);
System.out.println("Input your text:");
StringBuilder br = new StringBuilder();
String text;
for (;;) {
text = sc.nextLine();
if (text.equals(":exit")) {
break;
}
br.append(text);
br.append(System.lineSeparator());
}
saveToFile(br.toString(), file);
sc.close();
}
public static void saveToFile(String text, File file) {
try (PrintWriter pr = new PrintWriter(file)) {
pr.print((text));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Saved!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment