Author Topic: Guys, need some help. Pano ba mag-write and mag-read ng text files?  (Read 100 times)

0 Members and 1 Guest are viewing this topic.

Offline rpmolecule

  • Full Member
  • *
  • Posts: 231
  • Karma: +1/-0
  • Seize the Day!
    • View Profile
Guys, need some help. Pano ba mag-write and mag-read ng text files using java? May sample code ba kayo?

Techronnati | where technology never sleeps


Mountain View

Offline MrSpecialist

  • Sr. Member
  • *
  • Posts: 345
  • Karma: +1/-0
  • I'm an expert!
    • View Profile
Re: Guys, need some help. Pano ba mag-write and mag-read ng text files?
« Reply #1 on: July 21, 2016, 06:40:28 AM »
Actually maraming ways, pero just to give an idea share ko lang....  When you use JDK 7 meron ng available like Paths and Path. Sample ganito:

Sa pag read ng files:

Code: [Select]
1  package com.techronnati.qna;
2  import java.io.FileNotFoundException;
3  import java.io.FileReader;
4  import java.util.Scanner;
5  public class IOFilesSample {
6  public static void main(String[] args) {
7  try {
8   Scanner scan = new Scanner(new FileReader("c:\\tech\\file.txt"));
9   while(scan.hasNext()){
10    System.out.println(scan.nextLine());
11   }
12  } catch (FileNotFoundException e) {
13   e.printStackTrace();
14  }
15 }
16 }

Sa lines 7-14 na tayo mag concentrate kasi I'm assuming alam mo na yung the rest.  At line 7, naglagay tayo ng try kasi yung FileReader na object pwedeng wala sya maread.  So kailangan gumamit ng try-catch or magthrow ng Exception.  Pinili ko na lang ang mag try and catch. 

Sa line 8, dalawa ang nangyayari dito.  Meron tayo para sa FileReader, meron din para sa Scanner.  Yung sa FileReader, nagcreate tayo ng new object na FileReader then yung parameter sa loob ay yung path and file ng kung saan yung gusto mong basahin na file.  Sa case natin dito, c:\tech\file.txt ang path and file natin.  Then nag declare tayo ng new object ng Scanner at ang parameter natin na ginamit ay yung FileReader na kakacreate lang din natin. 

At lines 9-11, dito naman ipiprint natin lahat ng laman ng file na nabasa natin.  Hanggang meron pang next line (scan.hasNext()), hindi tayo mag e-exit sa loop na yun.  Jut remember, ang pagbasa ni scan ay line by line.  So sa unang scan.nextLine() ang babasahin nya ay yung unang line ng text file.  Sa second na iteration ang babasahin naman nya ay yung second na iteration etc.   

Here's for writing files:


1 package com.techronnati.qna;
2 import java.io.BufferedWriter;
3 import java.io.FileWriter;
4 import java.io.IOException;
5 public class IOFilesSample {
6 public static void main(String[] args) {
7  try {
8         BufferedWriter out = new BufferedWriter(new FileWriter("c:\\tech\\file2.txt"));
9         out.write("hi and hello from\n");
10         out.write("Pinoy Java Tutorial");
11         out.close();
12     } catch (IOException e) {
13      e.printStackTrace();
14     }
15 
16 }
17
18 }

Let's start sa line 8.  Kagaya kanina, dalawa nangyayari dito.  Una, we constructed a FileWriter object and it's pointed at c:\tech directory then ang filename ay file2.txt kaya sya c:\tech\file2.txt.  Then meron tayong BufferedWriter na object.  Kailangan natin to para makapag write ng file. 

Then the rest ng lines ay madali lang.  Line 9 will write "hi and hello from" sa file.  Yung "\n" ay hindi makakasama kasi it means "new line" if you'll remember yung mga escape characters sa Java.  Line 10 will again write to the file.  This time "Pinoy Java Tutorial" naman.  And lastly, Line 11 closes the stream. 

So ang output nyan ay:

hi and hello from
Pinoy Java Tutorial

Now if wala yung "\n", ang magiging output ay:

hi and hello fromPinoy Java Tutorial

Kasi nga nawala yung new line.

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
1103 Views
Last post November 27, 2008, 02:03:28 AM
by MrSpecialist
0 Replies
50 Views
Last post April 02, 2010, 09:31:35 AM
by Berto
0 Replies
59 Views
Last post October 31, 2016, 03:56:41 AM
by Corps
0 Replies
42 Views
Last post January 26, 2017, 12:43:22 AM
by CyborgRepublic
1 Replies
170 Views
Last post March 14, 2017, 12:42:03 PM
by MrSpecialist

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.