UpdateFile.java(更新指定文件夹的文件)作者:阿飞

/*
 * 创建日期 2004-12-14
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package net.selfcafe;

import java.io.*;
/**
 * @author Administrator
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class  UpdateFile
{
 
 //遍历整个文件加
 public  static void SearchAllDec(String path) {
  
  int size;
  String subpath="";
  
  File newfile = new File(path);

  if (newfile.isDirectory()) {

   //System.out.println(newfile.getName());
   String filelist[] = newfile.list();
   size = filelist.length;
   for (int i = 0; i < size; i++) {
    subpath = path + "/" + filelist[i];
    SearchAllDec(subpath);
   }
  } else if (newfile.isFile()) {
   
   //得到文件路径
   String strFilePath = newfile.getAbsolutePath();
   String strFileContent = "";
   String strFileName = newfile.getName();
   String strFileExt = strFileName.substring( strFileName.length()-3,strFileName.length() );
   //如果是asp文件,得到文件中的内容
     
   strFileContent = getStrFromFile(strFilePath);
       
   if (strFileExt.equals("asp")){
    
    MakeNewFile(path,strFileName,strFileContent); 
   }
   
   
  } else {
   System.out.println("no such file or directory");
  }

  
 }
 
 /*
  *
  * 得到文件的内容
  * */
 public static String  getStrFromFile(String strFileNameAndPath)
    {
     
  BufferedReader br= null;
   
  StringBuffer s=new StringBuffer();
  String line = "";
  int iLineCount = 0;
  int iSum = 0;
  try{
   br = new BufferedReader(new FileReader(strFileNameAndPath));
   iLineCount = 0;
   
   while( (line=br.readLine())!=null)
   {
     //处理页面如果,如果包含框架的去掉    
    iLineCount ++;
    String strResult;   //临时字符串
    //boolean iChangeFlag = false;//保证成对出现
    strResult = "<iframe src=/"/ini/top.asp?pid=<%=pid%>/" frameborder=/"0/" scrolling=/"no/" width=/"778/" height=/"145/">";
     if (line.indexOf(strResult)>-1)
     {
      System.out.println("Line:"+ iLineCount);
      System.out.println(line);
      iSum++;
      line = "<td>/n";
      
    
     }
       strResult = "</iframe></td>";
       if (line.indexOf(strResult)>-1)
      {
        System.out.println("Line:"+ iLineCount);
       System.out.println(line);
       line = "</td>/n";
      }    
     s.append(line);
     s.append("/n");
   }
   System.out.print(iSum+ "");
   
  }catch(Exception e){
   
  }
  
  

      String result = new String(s);
      result = result.trim();     
     return result;
    }
 /*
  *  ==生成新的页面
  *
  * */
  
 public static boolean MakeNewFile(String strFilePath ,String strFileName,String newsContent){
  File path=null;
  File d=null;
  //System.out.println(strNewsPath);
  try
  {   
    path=new File(strFilePath);
    //System.out.println("1");
   if(!path.exists())
    path.mkdir();
    
    System.out.println(" 文件名称:"+strFilePath);
    //System.out.println(strFileName);
    String  tempStr = strFilePath.trim();
   
   //写入html文件
      FileWriter fw;
     try
     {
                 fw=new FileWriter(tempStr);//建立FileWriter对象,并实例化fw
        //将字符串写入文件
           fw.write(newsContent);
           fw.close();
       }
       catch(Exception e)
          {
         e.printStackTrace();
             }
    
  }catch(Exception e){
   System.err.println(e);
   e.printStackTrace();
   return false;
  }finally{
   path=null;
   d=null;
  }
  
  return true;
 }
 
 public static void main(String[] args)
 {
  SearchAllDec("h:/test");
  
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java代码更新Github文件,你需要使用Github API来实现。下面是一个简单的Java代码示例,演示如何使用Github API更新文件。 ``` import org.apache.commons.codec.binary.Base64; import org.eclipse.egit.github.core.Commit; import org.eclipse.egit.github.core.Repository; import org.eclipse.egit.github.core.RepositoryContents; import org.eclipse.egit.github.core.service.ContentsService; import org.eclipse.egit.github.core.service.RepositoryService; import org.eclipse.egit.github.core.util.EncodingUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; public class GithubApiExample { private static final String REPO_OWNER = "your_github_username"; private static final String REPO_NAME = "your_github_repository_name"; private static final String FILE_PATH = "path_to_your_file_in_repository"; private static final String ACCESS_TOKEN = "your_github_personal_access_token"; public static void main(String[] args) throws IOException { RepositoryService repositoryService = new RepositoryService(); repositoryService.getClient().setOAuth2Token(ACCESS_TOKEN); Repository repository = repositoryService.getRepository(REPO_OWNER, REPO_NAME); ContentsService contentsService = new ContentsService(); contentsService.getClient().setOAuth2Token(ACCESS_TOKEN); RepositoryContents file = contentsService.getContents(repository, FILE_PATH); String decodedContent = new String(Base64.decodeBase64(file.getContent()), StandardCharsets.UTF_8); String newContent = "Hello, Github API!"; Commit commit = new Commit(); commit.setMessage("Update file via Github API"); String encodedContent = EncodingUtils.toBase64(newContent.getBytes()); contentsService.updateFile(repository, FILE_PATH, commit, encodedContent, file.getSha()); } } ``` 在这个示例中,我们使用了Github API的Java库,Eclipse EGit。这个库提供了对Github API的访问。 首先,我们需要获取访问Github API所需的访问令牌。然后,我们使用RepositoryService和ContentsService连接到Github API。 接下来,我们获取要更新文件的内容。我们使用Base64编码将内容解码为字符串。 然后,我们创建一个新的字符串,并将其编码为Base64格式。最后,我们使用ContentsService的updateFile方法更新文件。我们提供了文件的路径、提交信息、新的内容和文件的SHA哈希值。 这是一个简单的Java代码示例,演示如何使用Github API更新文件。请注意,这个示例假定你已经安装了Eclipse EGit库,并且已经获取了Github API的访问令牌。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值