terça-feira, junho 07, 2005

NC Java and Files

PATH = infile[0];

try {//i/o may go wrong
LINE = new BufferedReader(new FileReader(PATH)); //input file handle
OUT = new PrintWriter(new FileWriter(PATHOUT)); //output file handle

while((data = LINE.readLine()) != null){ //read lines
System.out.println("Read: "+data); //..do some work
data = data.toUpperCase();
OUT.println(data); //write line
}
\tLINE.close(); \t//be nice.
\tOUT.close();

NC Making A Zip File With Java / FileOutputStream

Now for some exciting stuff. I'm going back to baby steps because we're introducing new concepts including FileOutputSteam and ZipOutputStream. The changes in the example occur in the main method. By opening a file output stream, a file is created on the hard drive. I then create a zip output stream and connect it to the file output stream. Get it? Now, anything I write to the zos object will be zipped! Later, I'll introduce the method for writing to zos. For now, it is important to immediately close the output streams after they're no longer needed (after the dirFunc method), because it is not good to leave files open. If you compile and run this example, you'll end up with a new zip file on your hard drive with no contents, named something like Pix_00-06-10_05-39-PM.zip.


import java.io.*;
import java.io.File;
import java.util.Calendar;
import java.util.zip.*;

/**
* Recursively list files, and make a zip file.
*/
class zipdir {
public static void main(String[] args) {
if (args.length < 1) {

System.out.println ("usage: java zipdir [sourcedir]");

} else {
// Call the dateFunc method.
String dateName = dateFunc();

// Must use try because file io is occurring.
try {
// Create the file output streams for both the file and the zip.
FileOutputStream fos = new FileOutputStream(args[0]+"_"+dateName+".zip");
ZipOutputStream zos = new ZipOutputStream(fos);

dirFunc(args[0]);

// Close the file output streams for both the file and the zip.
zos.close();
fos.close();
} catch (IOException e) {
} // end try

} // else
} // main

// New dirFunc method (not part of main method).
public static void dirFunc (String dirName) {
File dirObj = new File (dirName);
if (dirObj.exists() == true) {
if (dirObj.isDirectory() == true) {
// Create an array of File objects, one for each file or directory in dirObj.
File [] fileList = dirObj.listFiles();
// Loop through File array and display.
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
dirFunc (fileList[i].getPath());
} else if (fileList[i].isFile()) {
// Call the zipFunc function
zipFunc(fileList[i].getPath());
}
} // for loop
} else { System.out.println (dirName+" is not a directory."); }
} else { System.out.println ("Directory "+dirName+" does not exist."); }
}

// New zipFunc method.
public static void zipFunc (String filePath) {
System.out.println(filePath);
}

// New dateFunc method.
public static String dateFunc () {

// this creates a Calender object full of current day information.
Calendar calendar = Calendar.getInstance();
// This sequence sets String objects.
// The +"" forces a conversion to String objects.
String YY = (calendar.get(Calendar.YEAR)+"").substring(2);
String MM = ((calendar.get(Calendar.MONTH)+1)+"");
String DD = (calendar.get(Calendar.DAY_OF_MONTH)+"");
String HH = (calendar.get(Calendar.HOUR)+"");
String MI = (calendar.get(Calendar.MINUTE)+"");

// The AM_PM field contains 0 or 1, which I have to convert to AM or PM.
int AMint = (calendar.get(Calendar.AM_PM));
String AMStr;
if (AMint == 0) {AMStr = "AM"; } else {AMStr = "PM"; }

// And I do this for consistant date appearance. For example, 00-06-10_04-42-PM.
if (MM.length() == 1) { MM = "0"+MM; }
if (DD.length() == 1) { DD = "0"+DD; }
if (HH.length() == 1) { HH = "0"+HH; }
if (MI.length() == 1) { MI = "0"+MI; }

return (YY+"-"+MM+"-"+DD+"_"+HH+"-"+MI+"-"+AMStr);
} // end dateFunc

} // end zipdir class



LX VoIP na cabeça garoto

http://www.voip.ufsc.br/

http://www.teleco.com.br/tutoriais/tutorialtelip/default.asp

http://www.gnugk.org/gnugk-manual-pt-3.html

WIN url não abre com IE ou Mozilla Firefox

Ir em opções de pasta na aba Tipos de Arquivo

Procurar uma linha que tenha na descrição

(NENHUM) Atalhos da internet

e retirar todas operações

depois procurar a linha da extensão URL

retirar todas opções inclusive associação a um determinado
programa

Depois procurar a linha (NENHUM) URL:Protocolo HTTP

e colocar a ação open

e apontar para o {path}IE %1
ou {path} mozilla -url %1

não precisa colocar DDE
ele cria automaticamente

e ícone também se troca a partir dessa linha

This page is powered by Blogger. Isn't yours?