Lowden

now

JAVA常用基础代码

java常用功能代码:


//实现cmd命令的执行,调用记事本
Runtime runtime = Runtime.getRuntime();
String[] commandArgs = { "notepad.exe", "说明.txt" };
runtime.exec(commandArgs);


//实现String存放到txt文件中去
FileOutputStream ofs = new FileOutputStream("页面不规范URL.TXT");
String HtmlStr = pr.getURL() + "\n\r";
char[] ch = HtmlStr.toCharArray();
for (int i = 0; i < ch.length; i++) {
ofs.write(ch[i]);
ofs.flush();
}
ofs.close();


//完成远端数据下载
URLConnection uc = cm.openConnection(mp3url);
InputStream is = uc.getInputStream();
File outputfile = new File(tempmp3name);
OutputStream os = new FileOutputStream(outputfile);
while ((tempc = ism.read()) != -1) {
os.write(tempc);
os.flush();
}
ism.close();
os.close();


//检测本地文件是否存在
File file=new File("2009-4-27.txt");
f(file.exists()){
// file.delete();
System.out.println("2009-4-27.txt"+"文件已删除!");
}else{
System.out.println("2009-4-27.txt"+"文件不存在,没有删除!");
}


//建立目录与中文写入
  if(path.equalsIgnoreCase("null")){
   File   theFile   =   new   File(".\\Result");  
     if   (!theFile.exists())  
     {  
         theFile.mkdirs();  
     }
   path = new StringBuffer().append("Result\\"+title.trim()+".txt").toString();


  }else{
   File   theFile   =   new   File(".\\"+path);  
     if   (!theFile.exists())  
     {  
         theFile.mkdirs();  
     }
   path = new StringBuffer().append(path+title.trim()+".txt").toString();
  }
         BufferedWriter   be=new   BufferedWriter(new FileWriter(path));  
         be.write(sb.toString());      
          be.close();
  System.out.println ("正在写文件:"+path);

 

//向SOCKET里面写文件并监听SOCKET响应
 public boolean writeSocket(StringBuffer requestData){
  try{
   
   this.responseData = null;
   this.responseData = new StringBuffer();
   //写 
   OutputStream socketOut = socket.getOutputStream();   
       socketOut.write(requestData.toString().getBytes()); 
       socketOut.flush();   
   //读
       BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   String data ="null";
   while(true) {
    data = br.readLine();
    this.responseData = this.responseData.append(data);
    if(data==null)break;
   } 
   br.close();      
       return true;
  }catch(Exception e){
   System.out.println ("Socket data write fail");
   e.printStackTrace();
   return false;
  }
       
 }

posted on 2009-11-05 21:55 Lowden 阅读(172) 评论(0)  编辑  收藏 所属分类: Java - HtmlParser

My Links

Blog Stats

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

好友链接

搜索

最新评论

阅读排行榜

评论排行榜