• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt in Files

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey java Rocks,

Can you pls help me in this code. Actually Iam reading a file and printing the file.

Iam getting all the file content line by line into a string and printing the string.

But my prob is iam not able to repeat the loop every time. Is there any pre defined meth to do that..

this is the code..

import java.io.*;

class FileDemo
{
public static void main(String[] args)throws IOException
{
FileInputStream f = new FileInputStream(args[0]);
DataInputStream d = new DataInputStream(f);
BufferedReader b = new BufferedReader(new InputStreamReader(d));

if(args.length == 1)
{
try
{

while(???)
{
String str = b.readLine();
System.out.println(str);
}

}
catch(Exception e)
{
System.out.println("Caught");
}

finally
{
d.close();
f.close();
System.out.println("Completed");
}
}
else{System.out.println("Invalid parameters");}
}
}
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usual approach would be something like this:


Note that if the file contains text, you should not use a DataInputStream. It should just be
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic