0% found this document useful (0 votes)
30 views16 pages

BAD

Uploaded by

nadiadeka14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views16 pages

BAD

Uploaded by

nadiadeka14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package latihan;

import java.io.*;

/**

* @author Nindia

*/

public class FindCertainExtension {

private static final String FILE_DIR = "c:\\folder";

private static final String FILE_TEXT_EXT =".jpg";

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

new FindCertainExtension().listFile(FILE_DIR, FILE_TEXT_EXT);

public void listFile(String folder, String ext){

GenericExtFilter filter = new GenericExtFilter(ext);

File dir = new File(folder);

if(dir.isDirectory()==false){

System.out.println("Directory does not exists: "+ FILE_DIR);

return;

}
String[] list =dir.list(filter);

if(list.length == 0){

System.out.println("no files end with : "+ ext);

return;

for (String file : list){

String temp = new StringBuffer(FILE_DIR).append(File.separator)

.append(file).toString();

System.out.println("file : " + temp);

public class GenericExtFilter implements FilenameFilter{

private String ext;

public GenericExtFilter(String ext) {

public boolean accept(File dir, String name){

return (name.endsWith(ext));

}
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugas.pkg4;

import java.io.File;

/**

* @author Nindia

*/

public class test {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

File file = new File("c:\\");

if(file.exists()){

double bytes = file.length();

double kilobytes = (bytes / 1024);

double megabytes = (kilobytes / 1024);

double gigabytes = (megabytes / 1024);

double terabytes = (gigabytes / 1024);

double petabytes = (terabytes / 1024);

double exabytes = (petabytes / 1024);


double zettabytes = (exabytes / 1024);

double yottabytes = (zettabytes / 1024);

System.out.println("bytes : " + bytes);

System.out.println("kilobytes : " + kilobytes);

System.out.println("megabytes : " + megabytes);

System.out.println("gigabytes : " + gigabytes);

System.out.println("terabytes : " + terabytes);

System.out.println("petabytes : " + petabytes);

System.out.println("exabytes : " + exabytes);

System.out.println("zettabytes : " + zettabytes);

System.out.println("yottabytes : " + yottabytes);

}else{

System.out.println("FILE DOES NOT EXISTS");

}
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugas5;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.LineNumberReader;

/**

* @author Nindia

*/

public class StringToInputStreamExample {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

try{

File file = new file("c:\\ihave101ines.txt");

if(file.exists()){

FileReader fr = new FileReader(file);

LineNumberReader int = new LineNumberReader(fr);


int linenumber = 0;

while (Line.readerLine() !=null){

linenumber++;

System.out.println("Total number of lines : " + linenumber);

int.close();

}else{

System.out.println("FILE DOES NOT EXISTS!");

}catch(IOException e){

e.printStackTrace();

}
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugas.pkg6;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

/**

* @author Nindia

*/

public class Tugas6 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try{

File fileDir = new File("c:\\temp\\test.txt");

BufferedReader in = new BufferedReader{

new InputStreamReader{
new FileInputStream(fileDir), "UTFB"));

String str;

while(str = in.readLine()) !=null){

System.out.println(str);

in.close();

catch(UnsupportedEncodingException e)

System.out.println(e.getMessage());

catch(IOException e){

System.out.println(e.getMessage());

catch (exception e){

System.out.println(e.getMessage());

}
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugas.pkg7;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException

import java.io.Writer

/**

* @author Nindia

*/

public class Tugas7 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

try{

file fileDir = new File("c:\\temp\\test.txt");

Writer out = new BufferedWriter(new OutputStreamWriter(new FileoutputStream(fileDir),


"UTFS"));

out.append("website UTF-8").append("\r\n");

out.append("?? UTF-8").append("\r\n");
out.append("??????? UTF-8").append("\r\n");

out.flush ();

out.close();

catch (UnsupportedEncodingException e)

System.out.println(e.getMessage());

catch (IOException e)

System.out.println(e.getMessage());

}
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugas.pkg8;

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

/**

* @author Nindia

*/

public class Tugas8 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

String str = "This Is a String = GoGoGo";

InputStream is = new ByteArrayInputStream(str.getBytes());

BufferedReader br = new BufferedReader (new InputStreamReader(is));

String line;

while ((line = br.readLine()) !=null) {


System.out.println(line);

br.close();

}
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugas.pkg9;

import java.io.File;

/**

* @author Nindia

*/

public class DiskToSpaceDetail {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

File file = new File("c:");

long totalSpace = file.getTotalSpace();

long UsableSpace = file.getUsableSpace();

long freeSpace = file.getFreeSpace();

System.out.println("=== Partition Detail ===");

System.out.println("=== bytes ===");

System.out.println("Total size : " + totalSpace + "bytes");

System.out.println("Space free : " + UsableSpace + "bytes");


System.out.println("Space free : " + freeSpace + "bytes");

System.out.println("=== mega bytes ===");

System.out.println("Total size: " + totalSpace /1024/1024 + "sb");

System.out.println("Space free: " + UsableSpace /1024/1024 + "sb");

System.out.println("Space free: " + freeSpace /1024/1024 + "sb");

}
package LineNumberReaderExample;

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

public class tugasku {

public static void main(String[] args) throws IOException {

RandomAccessFile input = null;

String line = null;

try{

File file = new File("d:\\nadiadeka.txt");;

input = new RandomAccessFile(file, "r");

while ((line = input.readLine()) != null){

System.out.println(line);

return;

catch (Exception e){

System.out.println(e.getMessage());

finally{
if (input != null){

input.close();

You might also like