how to remove lines based on integer n?

I'm trying to alternate a file but keeping some lines intact based on a user input. (Details below code)

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

    // prompt for input file name
    Scanner console = new Scanner(; 
    System.out.print("Type first file name to use: ");
    String filename1 = console.nextLine();
    System.out.print("Type second file name to use: ");
    String filename2 = console.nextLine();
    System.out.println("enter an integer: ");
    int n = console.nextInt();
    Scanner input = new Scanner(new File(filename1));   //put the first file as input
    PrintStream output = new PrintStream(new File(filename2)); //put the second file as output
    int count =0;
        count ++;
            output.println(); // this is where i don't know what to place


the program should prompt the user to enter 2 file names and an integer n. it should create a second file that contains the first n lines of the first file, while keeping it intact. If the first file contains less than n lines then the second file will contain all the lines of the first file. i've started writing the while loop, but i am not sure what command i should include in order to have the desired output. thank you.


Have a try with the following code:

 int count = 0;
    while (count < n) {
        if (input.hasNextLine()) {
            output.println(input.nextLine()); // this is where i don't know what to place


     * Close scanner 


   count ++;
   } else {

You should also check the the input file exists. And also you should close your input and output. I will not matter in this trivial case but it's something you should do in more complex applications.

You should open PrintStream with the append option set to true (which is false by default) -

PrintStream output = new PrintStream(new File(filename2, **true**));

Then you can do something like below -

String inputLine = scanner.readLine(); 

Need Your Help

Capture multiple blog posts and concatenate

parsing website

I'm not sure what language fits this best (or if there's already a program for this), but here's what I basically want to do: When given a URL, I want it to go to that page, capture text between ce...

call jquery function inside datalist control

javascript jquery html css

My issue is i want to open the div onclick the anchor tag inside data-list control . i search on the google to reach to this function but it is not working

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.