|
|
| initialise array by reading from file |
Expert:Nish
Hello, I wnat to know how i would initialise an array by reading a text file, which contains a simple pattern.
for example the file may look as shown below, with the star character.
******* *** |
| Answers |
Hi friend,
Code to solve the problem :
import java.io.*; class FileRead { public static void main(String args[]) { try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("textfile.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console int strlen = strLine.length(); String strAr[] = new String[strlen]; //System.out.println("Intialize Array : " + strAr.length); for(int i=0;i<strAr.length;i++) { System.out.print(strLine.charAt(i)); } System.out.println(); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }
Thanks
|
thank you for ur post very helpful :D
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|