Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to get input from user.
input of 20 lines.
want to save it in a text file.
every line should occur in a new line.
thats it.
thanks.
Posted

You should take a help from this documentation[^].

Still,

get the input using Scanner and do the following,
try {
 
			String content = "This is the content to write into file";
// get you content here using Scanner
 
			File file = new File("C:/file1.txt");
 
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
 
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();
 
			System.out.println("Done");
 
		} catch (IOException e) {
			e.printStackTrace();
		}

-KR
 
Share this answer
 
In C you have stdio.h which contains all standard io functionality:
In your scenario you would require:
printf() to write output to the screen.
scanf() to read input from keyboard.
For file operations you require a couple more steps:
FILE *myfile;
myfile = fopen("myfile.txt", "w"); // or r, a, r+, w+, a+
fprintf(myfile, "Whatever...\n");
fclose(myfile); // always
 
Share this answer
 
Resp Person,
Program Accept 20 lines, to change line change loop count.
After press enter button, that line gets write to file.

-----------------Try This code-----------------------
Java
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Demo 
{
	public static void main(String[] args) 
	{
		try 
		{
		      //create a buffered reader that connects to the console
		      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		      //create an print writer for writing to a file
		      PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
		      		      
		      for(int i =0; i < 20; i++)		      		     
			      //output to the file a line
		    	  out.println(in.readLine());
		      
		      //close the file (VERY IMPORTANT!)
		      out.close();
		   }
		   catch(IOException e1) 
		   {
		        System.out.println("Error during reading/writing");
		   }
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900