• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Files program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends ,

I have created a student marklist program in Java using files . The aim of the program is to read the contents from the console , calculate the total and display it in the console as well as write to a file . I am not able to write the contents into the file . I am not able to figure out the problem . Please let me know the details ....

Thanks in advance ....

import java.io.*;

public class u3
{
public static void main (String [] args) throws IOException
{
String name;
String mark1,mark2;
double m1,m2,total,letter;
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
BufferedWriter br2 = new BufferedWriter (new FileWriter("oo.txt"));
try
{
System.out.println("Enter the name:");
name=br.readLine();
System.out.println("Enter the mark1:");
mark1=br.readLine();
m1=Double.parseDouble(mark1);
System.out.println("Enter the mark2:");
mark2=br.readLine();
m2=Double.parseDouble(mark2);
total=m1+m2;
System.out.println(total);
while((letter=br.read()) != -1) {
br2.write((char) letter);
br2.flush(); }
System.out.println("written successfully");
}
catch(IOException e)
{
}
}
}
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like a java in general question.

What is the error that you have ? Please also post your code in CODE tags. It makes it easier to read.

Are you unable to find the file because you specified a relative path instead of an absolute one ?
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First: why are you using these classes to do this?
The only reason is if you are studying for the exam, because that are much better classes for I/O.

You can't use the read method in the while loop. You got stuck in a infinite loop.
Append what you read from the console to a StringBuilder, then you iterate over the characters to write to the file.
 
reply
    Bookmark Topic Watch Topic
  • New Topic