Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007, 11:25 PM
Member
 
Join Date: Nov 2007
Posts: 9
renars1985 is on a distinguished road
java IO
I have following code.






import java.io.*;

public class Gramata {
public static void main (String[] args) {

int num_lines = 0;
File file = new File("D://myfile.txt");

// PrintWriter out = new PrintWriter(new FileWriter(myTextTxt));

// File myTextTxt = new File ("c://someDir//someAnotherDir//mytext.txt"); //<<<< šinī vietā pamēģini slash-us likt uz abām pusēm.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Vai jūs gribat apskatīt sarakstu(ja|ne)?");
if("ja".equalsIgnoreCase(reader.readLine())){ //<<<< šinī vietā reader ielasīs visu līdz ENTER nospiešanai.

try {

// Izveido buferizētu lasītāju lasīšanai no standartievades
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
// Izveido 'PrintWriter' instanci šim failam
PrintWriter out
= new PrintWriter(new FileWriter("myfile.txt"));
String s;

System.out.print("Ievadiet tekstu ");
System.out.println("[Ievades beigas: Ctrl-z]");

// Lasīt pa rindiņām un izvadīt tās failā
while ((s = in.readLine()) != null) {
out.println(s);
}

// Aizvērt konsoles lasītāju un faila rakstītāju
in.close();
out.close();

} catch (IOException e) {
// Apstrādā I/O izņēmumus
e.printStackTrace();
}
}

else
try {

// Izveido buferizētu lasītāju, lai lasītu pa rindiņām
BufferedReader in = new BufferedReader(new FileReader("myfile.txt"));
String s;

// Nolasa rindiņu un atbalso to uz ekrāna
s = in.readLine();
while ( s != null ) {
System.out.println("Read: " + s);
s = in.readLine();
}
// Aizver buferizētu lasītāju, kurš aizver arī faila lasītāju
in.close();

} catch (FileNotFoundException e1) {
// Ja fails neeksistē
System.err.println("File not found: " + file);
//System.err.println("File not found: " + file);

} catch (IOException e2) {
// Apstrādā citus I/O izņēmumus
e2.printStackTrace();
}


}
}





I can`t compile useing Eclipse.
There is error in line 15:

java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException

at Gramata.main(Gramata.java:15)




Actually I want execute if or else block in case of input parameter.


Can somebody help me? Thanks!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007, 01:39 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.io.*; public class Gramata { public static void main (String[] args) { int num_lines = 0; File outFile = null; String response = "ne"; try { outFile = new File("myfile.txt"); if(!outFile.exists()) outFile.createNewFile(); BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Vai jūs gribat apskatīt sarakstu(ja|ne)?"); response = reader.readLine(); reader.close(); } catch(IOException e) { System.out.println("Read error: " + e.getMessage()); } if("ja".equalsIgnoreCase(response)) { //<<<< šinī vietā reader ielasīs visu līdz ENTER nospiešanai. File file = null; try { // Izveido 'PrintWriter' instanci šim failam file = new File("Gramata.java"); BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(file))); PrintWriter out = new PrintWriter(new FileWriter(outFile)); String s; System.out.print("Ievadiet tekstu "); System.out.println("[Ievades beigas: Ctrl-z]"); // Lasīt pa rindiņām un izvadīt tās failā while ((s = in.readLine()) != null) { out.println(s); } // Aizvērt konsoles lasītāju un faila rakstītāju in.close(); out.close(); } catch (FileNotFoundException e1) { // Ja fails neeksistē System.err.println("File not found: " + file); } catch (IOException e) { // Apstrādā I/O izņēmumus e.printStackTrace(); } } else { // ne try { // Izveido buferizētu lasītāju, lai lasītu pa rindiņām BufferedReader in = new BufferedReader( new FileReader(outFile)); String s; // Nolasa rindiņu un atbalso to uz ekrāna s = in.readLine(); while ( s != null ) { System.out.println("Read: " + s); s = in.readLine(); } // Aizver buferizētu lasītāju, kurš aizver arī faila lasītāju in.close(); } catch (IOException e2) { // Apstrādā citus I/O izņēmumus e2.printStackTrace(); } } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 04:14 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org