-OquzHan- Admin
Mesaj Sayısı : 120 Reputation : 3 Doğum tarihi : 04/02/99 Kayıt tarihi : 29/09/10 Yaş : 25 Nerden : Adanaaa
Cüzdan Altın: 5000 Para: 6291
| Konu: Özel Java Kodları Paz Ekim 03, 2010 8:52 pm | |
| Dikkat: Javascript Kodları Değil Java Kodları arkadaşlar elimde bulunan java kodlarını paylaşmak istedim. eğer forumda verildiyse özür dilerim.. Basit Bir Http İstemcisi Örneği - Kod:
-
import java.net.*; import java.io.*;
public class SocketCat {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) { int port = 80; String file = "/"; try { URL u = new URL(args[i]); if (u.getPort() != -1) port = u.getPort(); if (!(u.getProtocol().equalsIgnoreCase("http"))) { System.err.println("I only understand http."); continue; } if (!(u.getFile().equals(""))) file = u.getFile(); Socket s = new Socket(u.getHost(), port); OutputStream theOutput = s.getOutputStream(); PrintWriter pw = new PrintWriter(theOutput, false); pw.println("GET " + file + " HTTP/1.0"); pw.println("Accept: text/plain, text/html, text/*"); pw.println(); pw.flush();
InputStream in = s.getInputStream(); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr, "ASCII"); String theLine; while ((theLine = br.readLine()) != null) { System.out.println(theLine); } } catch (MalformedURLException e) { System.err.println(args[i] + " is not a valid URL"); } catch (IOException ex) { System.err.println(ex); }
}
}
}
----------------------------------- Dosya Bölme İşlemi - Kod:
-
import java.io.*; import java.util.zip.*;
class Main { public static void main(String[] args) { if (args.length != 2) { System.err.println("Usage: java Main <input> <output>"); System.exit(-1); } try { FileInputStream in = new FileInputStream(args[0]);
// Create compressed stream that writes to byte array output stream ByteArrayOutputStream b = new ByteArrayOutputStream(); GZIPOutputStream out = new GZIPOutputStream(b);
byte[] buf = new byte[512]; int howmany; while ((howmany = in.read(buf)) > 0) { out.write(buf, 0, howmany); } in.close(); out.flush(); byte[] compressedBytes = b.toByteArray(); // get byte array b.writeTo(new FileOutputStream(args[1])); // write to file out.close(); // closes both out and b System.out.println("compressed size: " + compressedBytes.length); // ... do something with byte array
} catch (IOException e) { System.out.println(e); } } }
-------------------------------- Dosya Kopyalama - Kod:
-
import java.io.*;
public class Kopyala {
public Kopyala() { okuX(); }
private void okuX() { try { File kopyalanan=new File("C:\\command.com"); File kopyasi=new File("D:\" + kopyalanan.getName());
FileOutputStream yazici=new FileOutputStream(kopyasi); FileInputStream al=new FileInputStream(kopyalanan);
int b;
System.out.print("Dosya Boyutu:" + kopyalanan.length());
while ((b=al.read())!=-1) { yazici.write(b); }
} catch (Exception e) { // } }
public static void main(String[] args) { Kopyala a=new Kopyala(); } }
---------------------------------------- Dosyadan Son Satırı Silmek - Kod:
-
import java.io.*;
public class DelLstLine{ public static void main(String[] arg){ try{ RandomAccessFile raf = new RandomAccessFile("RandFile.txt", "rw"); long length = raf.length(); System.out.println("File Length="+raf.length()); //supposing that last line is of 8 raf.setLength(length - 8); System.out.println("File Length="+raf.length()); raf.close(); }catch(Exception ex){ ex.printStackTrace(); } }//end of psvm }//class ends
--------------------------------- Dosyaya Yazmak - Kod:
-
import java.io.*;
class Yaz { public static void main(String[] args) { try { FileOutputStream streamNesne = new FileOutputStream("Yeni Klasör\\tekstdosyasi.txt"); //FileOutputStream streamNesne = new FileOutputStream("c:\\Documents and Settings\\numan\\Desktop\\Yeni Klasör\\tekstdosyasi.txt");
PrintWriter yazdirici = new PrintWriter(streamNesne);
String satir="Merhaba Sevgili Okurlar, ";
yazdirici.println(satir); yazdirici.close();
System.out.println("Dilemis oldugunuz "" + satir + "" satiri ilgili dosyaya yazdirildi. "); }
catch (FileNotFoundException excep) { System.out.println("Bu isimde bir dosya bulunamadi ..."); } catch (IOException excep) { System.out.println("Bir "exception" olustu ..."); }
} }
---------------------- Dosyayı Kesip, Başka Yere Yapıştırmak - Kod:
-
public static void main(String args[]){ try{ // File (or directory) to be moved File file = new File("file.txt");
// Destination directory File dir = new File("gg");
// Move file to new directory boolean success = file.renameTo(new File(dir, file.getName())); if (!success) { System.out.println("File moved"); } } catch (Exception ioe){ ioe.printStackTrace(); } }
----------------------------- Etiketi Sürüklemek CTRL tuşuna basılı tutarak, fare ile etiketi sürükleyin! - Kod:
-
import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class MoveLabel extends JFrame { JLabel label; public MoveLabel() { label = new JLabel(new ImageIcon("copy.gif")); label.setBounds(20,30,16,19); label.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_DOWN) { label.setLocation(label.getX(),label.getY()+1); repaint(); } if(ke.getKeyCode() == KeyEvent.VK_UP) { label.setLocation(label.getX(),label.getY()-1); repaint(); } if(ke.getKeyCode() == KeyEvent.VK_LEFT) { label.setLocation(label.getX()-1,label.getY()); repaint(); } if(ke.getKeyCode() == KeyEvent.VK_RIGHT) { label.setLocation(label.getX()+1,label.getY()); repaint(); } } }); label.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { if(me.getClickCount() == 1) { boolean dd = label.isOptimizedDrawingEnabled(); boolean ff = label.requestFocusInWindow(); repaint(); } }
}); JPanel p = new JPanel(); p.setPreferredSize(new Dimension(2000,1000)); p.setLayout(null); p.add(label); JScrollPane js = new JScrollPane(p); getContentPane().add(js); } public static void main(String args[]) { MoveLabel frame = new MoveLabel(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.setSize(300,300); frame.setVisible(true); } }
i-Master.TurkForumPro.com -------------------------------------- Admin -OquzHan- | |
|