Mais adequado para iniciantes. Ainda há alguns problemas de lógica. Pode ser usado para aprender operações de arquivo java
Endereço para download: http://yun.baidu.com/share/link?shareid=4184742416&uk=1312160419
A seguir está o código principal de operação do arquivo JAVA
ArquivoHelp.java
pacote self.yy.filesystem.fileutil; importar android.content.Context; importar android.util.Log; importar android.widget.Toast; importar java.io.FileInputStream; importar java.io.FileNotFoundException; ;importar java.io.FileOutputStream;importar java.io.IOException;importar java.net.URI;importar java.nio.channels.FileChannel;importar java.text.SimpleDateFormat;importar java.util.ArrayList;importar java.util.Calendar;importar java.util.List /** * Classe de ajuda relacionada ao arquivo*/classe pública FileHelp {; final estático privado String TAG = "FileHelp"; final estático público String JPG = ".jpg"; final estático público String PNG = ".png"; .png"; final String MP3 = ".mp3"; public static final String MP4 = ".mp4"; public static final String APK = ".apk"; ISTXT = 0; private static String TXT = ".txt"; /** * Exclusão de arquivo*/ public static boolean deletfile(Arquivo file) { if (file.isDirectory()) { if (file.listFiles().length > 0) { for (Arquivo i: file.listFiles()) {deletfile(i); } else { file.delete(); file.delete(); return true } /** * Cria uma nova pasta* Return true A criação do arquivo foi bem-sucedida* Return false A criação do arquivo falhou -> o arquivo existe* Return true A criação do arquivo foi bem-sucedida, retorne false Falha na criação do arquivo (o arquivo existe, as permissões são insuficientes) */ public static boolean creatFile(String filename, String path) { File file = new File(path + File.separator + filename); false } else { file.mkdir(); return true } } /** * Crie um tipo de arquivo personalizado file * Qualquer pasta * 0 txt text * * @return boolean * Return true O arquivo foi criado com sucesso e false é retornado. A criação do arquivo falha (o arquivo existe e as permissões são insuficientes) * * */ public static boolean creatFile(String filename, String path, int type) { String ptr = path + File. separador + nome do arquivo; arquivo switch (tipo) { case 0: arquivo = novo arquivo (ptr + TXT); falso; } senão { try { file.createNewFile(); return true } catch (IOException e) { return false; o arquivo é criado* / public static boolean reName(String name, File file) { String pathStr = file.getParent() + File.separator + name; return file.renameTo(new File(pathStr) }); /** * Cópia do arquivo* * @param oldFile O arquivo a ser copiado* @param toNewPath O local para onde copiar* @return boolean trun se a cópia for bem-sucedida, false se a cópia falhar* * */ public static boolean copeyFile( Arquivo oldFile, String toNewPath) { String newfilepath = toNewPath + File.separator + oldFile.getName(); //Determine se existe um arquivo relativo no caminho do arquivo copiado. Se existir, pare a operação if (temp.exists()) { return false } //Determine se o tipo de arquivo copiado é uma pasta if (oldFile. isDirectory() ) { temp.mkdir(); for (File i : oldFile.listFiles()) { copeyFile(i, temp.getPath()); else { //Se for um arquivo, execute a cópia do pipe try); { //Cria um pipe a partir do fluxo de arquivos FileInputStream fis = new FileInputStream(oldFile); FileChannel creatChannel = fis.getChannel(); .getChannel() //Copiar arquivos (conexão de pipeline) getChannel.transferFrom(creatChannel, 0, creatChannel.size()); getChannel.close(); creatChannel.close(); "copey derrotado, arquivo mebey existia"); e.printStackTrace(); return false } } return true; O arquivo a ser cortado* @param newFilePath O local a ser cortado* @return boolean trun se o corte for bem-sucedido, false se o corte falhar*/ public static boolean cutFile(File oldFile, String newFilePath) { if (copeyFile(oldFile, newFilePath) ) { oldFile.delete(); return true } else { return false } } /** * Obtenha o conjunto de arquivos correspondente ao tipo de arquivo* * @param dir folder* @param type file type, formato ".xxx" * @return List<file> file set*/ public static List<File> getTheTypeFile(File dir, String tipo) { List<Arquivo> arquivos = new ArrayList<Arquivo>(); for (Arquivo i: dir.listFiles()) { String filesTyepe = getFileType(i); (type.equals(filesTyepe)) { files.add(i); } return files; } /** * Obtém o tipo de arquivo* * @param file O arquivo que precisa ser verificado * @return String file type* Tal como: * Passa o arquivo chamado "test.txt" * Retorna .txt * * */ public static String getFileType(Arquivo file) { String fileName = file.getName(); (fileName.contains(".")) { String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length()); Classe de horário da última operação do arquivo * * @param file Classe do arquivo a ser consultado * @return "yy/MM/dd HH:mm:ss" string de dados * Por exemplo: * 14/07/01 01:02:03 */ public static String getCreatTime(Arquivo file) { long time = file.lastModified(); ss"); String data = dateFormat.format(calendar.getTime()); data de retorno; } }
O texto acima é todo o conteúdo deste artigo. Espero que seja útil para todos que estão aprendendo Java.