115 Program to print every nth character from the file in Java

Code:
/*
Program that reads a text file and prints every nth
character from the file. 'n' should be passed as a
command line argument.
*/

import java.io.*;

class EveryNth
{
public static void main(String[] args) throws IOException , FileNotFoundException
{
FileInputStream fis = new FileInputStream("file.txt");

int n = Integer.parseInt(args[0]);

int i=0 , j=0 ;

int data = fis.read();

while(data != -1)
{

if(i == j)
{
System.out.print((char)data);
j = j +n;
}

i++;
data = fis.read();
}

fis.close();
}
}


Ouput:

print every nth character from the file in Java

Previous
Next Post »