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




Have Programming Question? Ask it here!
 
 
 


Gowrishankar
jaVA INT
1 Answer(s)      3 years and 4 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
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
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
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
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
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
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
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
SQL Add Column Int
SQL Add Column Int       SQL Add Column Int is used to add new column, whose field data type... Int example .In this Tutorial we create a table Stu_Table using create statement
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
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
Conversion from int to String
Conversion from int to String: In this tutorial we will learn how to convert an int type value to a String type. You will convert an int value  to String by using toString() method. Description: This program will take an int
Conversion from int to float
Conversion from int to float: In this tutorial we will learn how to convert an int type value  to float type. Description: This program will take two int values from console and provide the division of these int values 
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 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
Conversion from String to int
Conversion from String to int: In this tutorial we will learn how to convert a string type data to int type data. Description: This program will take a String value from mystring variable. The line int myint = Integer.parseInt
Conversion from int to short
Conversion from int to short: In this tutorial we will learn how to convert an int type value  to short type data. Description: This program will take an int value from console and provide a conversion to short type data
Conversion from int to long
Conversion from int to long: In this tutorial we will learn how to convert an int type value to long type data. Description: This program will take an int value from console and provide a conversion to long data. The line int myint
Conversion from int to byte
Conversion from int to byte: In this tutorial we will learn how to convert an int type value to byte type data. Description: This program will take an int value from console and provide a conversion  to byte data. The line int
Conversion from int to boolean
Conversion from int to boolean: In this tutorial we will learn how to convert an int type value to boolean type data. Description: This program will take an int value from console and provide a conversion to boolean type data(true
Conversion from int to char
Conversion from int to char: In this tutorial we will learn how to convert an int type value to char type data. Description: This program will take an int value from console and provide a conversion to char type data. The line int
Advertisements
 

 

 

DMCA.com