Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Gowrishankar
jaVA INT
1 Answer(s)      3 years and 11 months ago
Posted in : Java Magazine


Read a text file and precede the line with the line number and write it to another new text file.

For example if you create a file Source.txt which has the following contents: Hello there I am supposed to have line numbers preceding me. Is it there ?

shall be written to Destination.txt as: 1. Hello there 2. I am supposed to have line numbers preceding me. 3. Is it there?


Advertisement
View Answers

December 15, 2010 at 4:45 PM


Hi Friend,

Try the following code:

import java.io.*;
class FileExample{

    public static void main(String[] args)throws Exception{
        BufferedWriter bw=new BufferedWriter(new FileWriter("C:/destination.txt",true));
        LineNumberReader ln = new LineNumberReader(new FileReader(new File("C:/hello.txt")));
        String line = "";
        while ((line = ln.readLine()) != null){
        System.out.println("Line:  " + ln. getLineNumber() + ": " + line);
        bw.write("Line:  " + ln. getLineNumber() + ": " + line);
        bw.newLine();
      }
      bw.close();

    }
}

Thanks



Related Tutorials/Questions & Answers:
jaVA INT
jaVA INT  Read a text file and precede the line with the line number and write it to another new text file. For example if you create a file Source.txt which has the following contents: Hello there I am supposed to have line
int to byte in java
int to byte in java  How to convert int to byte in Java?   int i =132; byte b =(byte)i; System.out.println(b);   public static byte[] intToByteArray(int value) { byte[] b = new byte[4
Advertisements
How to rewind the int buffer in java.
How to rewind the int buffer in java. In this tutorial, we will discuss how to rewind the int buffer in java.  IntBuffer API... allocate( int capacity)  The allocate() method create a int
How to clear int buffer in java.
How to clear int buffer in java. In this tutorial, we will discuss how to clear int buffer in java.          ... C:\>java IntBufferClear Int value in the buffer. 22 23 24
one attribute of type int - Java Beginners
one attribute of type int  hi friends can anyone help me understand this question:- Define a class called Month. Your class will have one attribute of type int to represent a month (1 for January, 2 for February, and so
How to allocate a new int buffer in java.
How to allocate a new int buffer in java.  In this tutorial, we will see how to allocate a new int buffer. IntBuffer API: The java.nio.IntBuffer... allocate( int capacity)  The allocate() method allocate a int buffer
Java arraylist int
. In this example int data type gets converted into Integer data type. Example of Java Arraylist int import java.util.ArrayList; import java.util.List... arraylist can contain int data type elements also.  Only
java.lang.String.valueOf(int inum)
The valueOf(int inum) method in Java used for converting int value into string. Basically, inum converts the int value into a String. See the example below...) { String Str = String.valueOf(555); // int values
How to convert excel file int xml format in java
How to convert excel file int xml format in java  How to read excel file(xls) which is stored in any directory(D://) and convert it into xml format in java and place in any directory(E://).Can any body provide the code
Java char to int
Java char to int We are going to describe How to convert char to int. First... char to int. If we parse the string value it will display a single integer...); String s = character.toString(); int i = Integer.parseInt(s
String substring(int beginIndex, int endIndex)
the substring(int beginIndex, int endIndex) method through the following java program... String substring(int beginIndex, int endIndex)  ... explanation about the substring(int beginIndex, int endIndex) method of String
Java Parse int Exception
Java Parse int Exception     ... Used in parseInt(java lang.String,int )method are  1) s- Stand for string... java.lang.integer.public static method int parseInt(String s) gives you NumberFormatException
How to read all value from int buffer in java.
How to read all value from int buffer in java. In this tutorial, we will discuss how to read all value from int buffer in java.  IntBuffer API... static IntBuffer allocate( int capacity)  The allocate() method
Int and Bint
Int and Bint   hello, What is different between int and bint?   hii, nice Question bigint can hold values from -2(race...= (9,223,372,036,854,775,807) and it takes 8 bytes but in case of int int can hold values
The int keyword
; The int is a keyword in java that is used to define 32-bit signed integer... specific meaning relevant to a compiler. The keyword int in java programming language... of integer type. The java.lang.Integer class in java programming language wraps a int
where is the definition of keywords like this, int, void present in Java ? e.g the definition of methods are in API.
where is the definition of keywords like this, int, void present in Java ? e.g the definition of methods are in API.  where is the definition of keywords like this, int, void present in Java ? e.g the definition of methods
range of int in c
range of int in c  What is the range of int in c/c++ and Java programming language
String lastIndexOf(int ch)
about the lastIndexOf(int ch) method through the following java program... String lastIndexOf(int ch)     ... the lastIndexOf(int ch) method of String class. We are going to use lastIndexOf(int
String copyValueOf(char[] data, int offset, int count)
String copyValueOf(char[] data, int offset, int count...[] data, int offset, int count) method of String class in Java...:\unique>java StrCopyValueOf The new String contains "6" C
How to convert a String to an int?
format. Now I want to convert into int value. Tell me How to convert a String to an int? Thanks   Hi, In Java its very easy and you can use...How to convert a String to an int?  I my program I am taking
Create a int array with the help of int buffer.
C:\>java IntBufferArray Int value in buffer. 23 24 Int...Create a int array with the help of int buffer.  In this tutorial, we will see how to create a int array with the help of int buffer. IntBuffer API
Transfer the content of a int array into int buffer.
;} } Output C:\>java ArrayToBuffer Content of int buffer. 2...Transfer the content of a int array into int buffer.  In this tutorial, we will see how to transfer the content of a int array into int buffer
php parse string as int
php parse string as int  While trying to pass string to int using int() i get the following error: Call to undefined function int() Why it is throwing an error
convert date time to int
convert date time to int  convert date time to int
Compare a int buffer with another int buffer.
Compare a int buffer with another int buffer.  In this tutorial, we will see how to compare a int buffer with another int buffer. IntBuffer API... static IntBuffer allocate(int capacity)  The allocate(..)method
Double To Int Without TypeCasting
Double To Int Without TypeCasting  How to convert Double to Int without using typecasting?   class ConvertDoubleToInt { public static void main(String[] args) { double d=25.5; int num=new
NSInteger from int
NSInteger from int  Hi, How to initialize NSInteger from int variable? Thanks   Hi, you might do it as shown below: NSInteger i = 10; Let's know if you have any better code. Thanks
conver object to int
conver object to int  i have a method returns object but i need int how can convert ? notice:object not means string means object string str map.get(str) returns object but i need an integer
Converting string to Int - JDBC
Friend, Use the following code to convert string to int. Integer i
Write a int value into int buffer at given index.
Write a int value into int buffer at given index.  In this tutorial, we will see how to write the given int value into int buffer at the given ... Method Description static IntBuffer allocate(int capacity
Create a int buffer by wrapping an int array into a buffer.
Create a int buffer by wrapping an int array into a buffer.  In this tutorial, we will see how to create a int buffer by wrapping an int array... Description static IntBuffer wrap(int[] array) 
Advertisements
 

 

 

DMCA.com