Gowrishankar
jaVA INT
1 Answer(s)      2 years and 5 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?

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 Pages:
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
range of int in c
range of int in c  What is the range of int in c/c++ and Java programming language
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
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
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
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
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
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
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) 
String lastIndexOf(int ch, int fromIndex)
explanation about the lastindexOf(int ch, int fromIndex) method of String class. We are going to use lastindexOf(int ch, int fromIndex) method of String class in Java... String lastIndexOf(int ch, int fromIndex)   
String indexOf(int ch, int fromIndex)
about the indexOf(int ch, int fromIndex) method of String class. We are going to use indexOf(int ch, int fromIndex) method of String class in Java... String indexOf(int ch, int fromIndex)   
Transfer the content of a int buffer into another int buffer.
;} } Output C:>java BufferToBuffer Int value in new buffer...Transfer the content of a int buffer into another int buffer.  In this tutorial, we will see how to transfer the content of a int buffer into another
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
Convert Object to Int
Convert Object to Int      ... Object into a primitive type int and Integer Object to primitive type int. . ... into primitive type int and Integer type object to primitive type int. The parseInt
How to transfer data from int buffer to int array.
How to transfer data from int buffer to int array.  In this tutorial, we will discuss how to transfer data from int buffer to int array. IntBuffer... static IntBuffer wrap(int[] array)   The wrap() method
Creates a read-only int buffer that shares the content of int buffer.
Creates a read-only int buffer that shares the content of int buffer. In this tutorial, we will see how to creates a read-only int buffer that shares the content of int buffer. IntBuffer API : The java.nio.IntBuffer class extends
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
Creates a duplicate int buffer that shares the content of int buffer.
Creates a duplicate int buffer that shares the content of int buffer. In this tutorial, we will see how to creates a duplicate int buffer that shares the content of int buffer. IntBuffer API : The java.nio.IntBuffer class extends
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
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
String substring(int beginIndex)
the substring(int beginIndex) method of String class. We are going to use substring(int beginIndex) method of String class in Java. The description of the code... String substring(int beginIndex)   
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
String indexOf(int ch)
the indexOf(int ch) method through the following java program. In the program... String indexOf(int ch)      ... the indexOf(int ch) method of String class. We are going to use indexOf(int ch
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 create a int buffer with the help of byte buffer.
How to create a int buffer with the help of byte buffer.  In this tutorial, we will see how to create a int buffer with the help of byte buffer... Description static ByteBuffer allocate( int capacity) 
Creates a view of  byte buffer as a int buffer.
Creates a view of  byte buffer as a int buffer.  In this tutorial, we will see how to creates a view of byte buffer as a int buffer. ByteBuffer... static ByteBuffer allocate(int capacity)  The allocate
Insert an element with the add(int, Object) method
Insert an element with the add(int, Object) method   ... an element at the specified position using the add(int, Object) method. Here is an example that provides the usage of the add(int, Object) method in more detail
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
String lastIndexOf(String str, int fromIndex)
the lastindexOf(String str, int fromIndex) method through the following java... String lastIndexOf(String str, int fromIndex)  ... the detailed explanation about the lastindexOf(String str, int fromIndex) method
String indexOf(String str, int fromIndex)
explanation about the indexOf(String str, int fromIndex) method of String class. We are going to use indexOf(String str, int fromIndex) method of String class in Java... String indexOf(String str, int fromIndex)   
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
String getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
String getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin... of String class in Java. The description of the code is given below for the usage...;javac getChar.java C:\unique>java getChar
How to get specific index value from int buffer.
How to get specific index value from int buffer. In this tutorial, we will discuss how  to get specific index value from int buffer. IntBuffer... static IntBuffer allocate( int capacity)  The allocate
Replace an object with set(int, Object) method
Replace an object with set(int, Object) method   ... that provides the usage of the set(int, object) method in more detail.  Create... objects using the add(int,object) method. Now replace an object from a particular
String to Int Conversion
String to Int Conversion       This section illustrates you how to convert a string into its integer...;} }   Here is the Output of the program: C:\>java
Java get Int from String
Java get Int from String       In this section, you will study how to obtain the integer value from String. For this, we have defined a string. The method  Integer.parseInt
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
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
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
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
Converting string to Int - JDBC
Friend, Use the following code to convert string to int. Integer i
Struts 2 Validation (Int Validator)
Struts 2 Validation (Int Validator)      ... for parent classes of the action and interfaces implemented. Using Java 5... Int Validator Regex Validator Required Validator Requiredstring
int Array
int Array       ... have declared an array of type int then the array will take only the int values... below: <HTML> <HEAD> <TITLE>Using int Array

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.