Created
March 16, 2014 16:55
-
-
Save Keda87/9586243 to your computer and use it in GitHub Desktop.
IO operation with Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PBO.IO.Reader; | |
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class ReaderTest { | |
public static void main(String[] args) { | |
try { | |
BufferedReader reader = new BufferedReader(new FileReader("baca.txt")); | |
String line; | |
while((line = reader.readLine()) != null) { | |
System.out.println(line); | |
} | |
reader.close(); | |
} catch (FileNotFoundException ex) { | |
Logger.getLogger(ReaderTest.class.getName()).log(Level.SEVERE, "file tidak ditemukan", ex); | |
} catch (IOException ex) { | |
Logger.getLogger(ReaderTest.class.getName()).log(Level.SEVERE, "kesalahan IO", ex); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PBO.IO.Writer; | |
import java.io.BufferedWriter; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class WriterTest { | |
public static void main(String[] args) { | |
try { | |
BufferedWriter writer = new BufferedWriter(new FileWriter("newfile.txt")); | |
writer.write("ini baris pertama\n"); | |
writer.flush(); | |
writer.write("ini baris kedua\n"); | |
writer.write("ini baris ketiga\n"); | |
writer.close(); | |
} catch (IOException ex) { | |
Logger.getLogger(WriterTest.class.getName()).log(Level.SEVERE, null, ex); | |
} finally { | |
System.out.println("tulis file selesai"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PBO.IO.ImageIO; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.logging.Logger; | |
import javax.imageio.ImageIO; | |
import javax.swing.Icon; | |
import javax.swing.ImageIcon; | |
import javax.swing.JOptionPane; | |
public class ImageTest { | |
private static final Logger LOGGER = Logger.getLogger(ImageTest.class.getName()); | |
public static void main(String[] args) { | |
try { | |
URL url = new URL("https://www.gravatar.com/avatar/7d48e514e24de81806f4660759fbe7fb?s=256&d=identicon&r=PG"); | |
URLConnection urlcon = url.openConnection(); | |
InputStream is = urlcon.getInputStream(); | |
OutputStream os = new FileOutputStream("gambar.jpg"); | |
int byteRead; | |
do { | |
byteRead = is.read(); | |
os.write(byteRead); | |
} while (byteRead != -1); | |
LOGGER.info("ambil data dari stream"); | |
os.flush(); | |
os.close(); | |
is.close(); | |
//tampilkan gambar | |
Icon icon = new ImageIcon(ImageIO.read(new File("gambar.jpg"))); | |
JOptionPane.showMessageDialog(null, null, "menampilkan gambar", | |
JOptionPane.INFORMATION_MESSAGE, icon); | |
LOGGER.info("menampilkan gambar"); | |
} catch (MalformedURLException ex) { | |
LOGGER.severe("malform url masbro.."); | |
} catch (IOException ex) { | |
LOGGER.severe("kesalahan input output masbro.."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PBO.IO.InputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class InputStreamTest { | |
public static void main(String[] args) { | |
try { | |
URL url = new URL("http://www.yahoo.com"); | |
URLConnection urlconn = url.openConnection(); | |
InputStream is = urlconn.getInputStream(); | |
int byteRead; | |
do { | |
byteRead = is.read(); | |
System.out.println((char) byteRead); | |
} while (byteRead != -1); | |
} catch (MalformedURLException ex) { | |
Logger.getLogger(InputStreamTest.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (IOException ex) { | |
Logger.getLogger(InputStreamTest.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PBO.IO.File; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class FileTest { | |
public static void main(String[] args) { | |
File f = new File("newfile.txt"); | |
if(!f.exists()) { | |
try { | |
f.createNewFile(); | |
System.out.println(f.getAbsolutePath()); | |
} catch (IOException ex) { | |
Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
File folder = new File("newfolder"); | |
if(!folder.exists()) { | |
folder.mkdir(); | |
//f.mkdirs(); //membuat folder dalam folder | |
} | |
File currentFolder = new File(System.getProperty("user.dir")); | |
File[] siblings = currentFolder.listFiles(); | |
for (File sibling : siblings) { | |
if(sibling.isFile()) { | |
System.out.println("file : " + sibling.getName()); | |
} else { | |
System.out.println("directory : " + sibling.getName()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PBO.IO.OutputStream; | |
import PBO.IO.InputStream.InputStreamTest; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class OutputStreamTest { | |
public static void main(String[] args) { | |
try { | |
URL url = new URL("http://www.google.com"); | |
URLConnection urlconn = url.openConnection(); | |
InputStream is = urlconn.getInputStream(); | |
OutputStream os = new FileOutputStream("google.txt"); | |
int byteRead; | |
do { | |
byteRead = is.read(); | |
os.write(byteRead); | |
System.out.println((char) byteRead); | |
} while (byteRead != -1); | |
os.close(); | |
} catch (MalformedURLException ex) { | |
Logger.getLogger(InputStreamTest.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (IOException ex) { | |
Logger.getLogger(InputStreamTest.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment