MrAwesome8
MrAwesome8

Reputation: 269

Is there any easy way to export user input into a text file?

Preferably in the same class?

I tried this: http://mrbool.com/how-to-store-data-with-java/25483

but it was to hard to manipulate my data.

Are there any other easier ways?

Thanks for any suggestions

Upvotes: 0

Views: 372

Answers (2)

DSlomer64
DSlomer64

Reputation: 4283

I don't know if this is anything like what you're after, but ...

package test;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Test {

  static String endOfInputDelimiter = "/";

  public static void main(String[] args) throws FileNotFoundException  {

    Scanner scan = new Scanner(System.in);
      PrintWriter p = new PrintWriter("outputFile.txt");
        while(scan.hasNext()){
          String s = scan.nextLine();
          if(s.equals(endOfInputDelimiter))
            break;
          p.println(s);
        }
      p.close();
    scan.close();
  }
}

It's pretty hokey; I especially don't like the "if...break", but maybe it's a start for you.

Upvotes: 0

La-comadreja
La-comadreja

Reputation: 5755

Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
PrintWriter p = new PrintWriter("outputFile.txt");
p.println(s);
...
p.close();

Scanner can grab user command-line input and store it as a String, which can easily be sent to a file.

Upvotes: 2

Related Questions