Skip to content

Commit

Permalink
see 08/20 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Aug 20, 2016
1 parent b77b22b commit 5ae8ac2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 100 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
> - 将输入流写入文件 *writeFileFromIS*
> - 将字符串写入文件 *writeFileFromString*
> - 简单获取文件编码格式 *getFileCharsetSimple*
> - 获取文件行数 *getFileLines*
> - 指定编码按行读取文件到List *readFile2List*
> - 指定编码按行读取文件到StringBuilder中 *readFile2SB*
> - byte单位转换(单位:unit) *byte2Unit*
> - 获取文件大小 *getFileSize*
> - 根据全路径获取最长目录 *getDirName*
> - 根据全路径获取文件名 *getFileName*
> - 根据全路径获取文件名不带拓展名 *getFileNameNoExtension*
> - 根据全路径获取文件拓展名 *getFileExtension*
> - **图片相关→[ImageUtils.java][image.java]**
> - 完善ing
Expand Down Expand Up @@ -205,7 +214,7 @@
***
Gradle:
``` groovy
compile 'com.blankj:utilcode:1.1.0'
compile 'com.blankj:utilcode:1.1.1'
```

### Proguard
Expand Down
2 changes: 1 addition & 1 deletion utilcode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 11
targetSdkVersion 23
versionCode 2
versionName "1.1.0"
versionName "1.1.1"
}
buildTypes {
release {
Expand Down
190 changes: 92 additions & 98 deletions utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.blankj.utilcode.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
Expand Down Expand Up @@ -433,7 +434,7 @@ public static boolean writeFileFromIS(File file, InputStream is, boolean append)
if (!createOrExistsFile(file)) return false;
OutputStream os = null;
try {
os = new FileOutputStream(file, append);
os = new BufferedOutputStream(new FileOutputStream(file, append));
byte data[] = new byte[KB];
while (is.read(data) != -1) os.write(data);
return true;
Expand Down Expand Up @@ -500,14 +501,14 @@ public static String getFileCharsetSimple(String filePath) {
*/
public static String getFileCharsetSimple(File file) {
int p = 0;
BufferedInputStream bin = null;
InputStream is = null;
try {
bin = new BufferedInputStream(new FileInputStream(file));
p = (bin.read() << 8) + bin.read();
is = new BufferedInputStream(new FileInputStream(file));
p = (is.read() << 8) + is.read();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeIO(bin);
closeIO(is);
}
switch (p) {
case 0xefbb:
Expand Down Expand Up @@ -558,63 +559,67 @@ public static int getFileLines(File file) {
}

/**
* 按行读取文件
* 指定编码按行读取文件到List
*
* @param file 文件
* @return 行链表
* @param filePath 文件路径
* @param charsetName 编码格式
* @return 文件行链表
*/
public static List<String> readFileByLine(File file) {
return readFileByLine(file, null);
public static List<String> readFile2List(String filePath, String charsetName) {
return readFile2List(getFileByPath(filePath), charsetName);
}

/**
* 按行读取文件
* 指定编码按行读取文件到List
*
* @param file 文件
* @param charsetName 字符编码格式
* @return 行链表
* @param charsetName 编码格式
* @return 文件行链表
*/
public static List<String> readFileByLine(File file, String charsetName) {
if (file == null) return null;
List<String> list = new ArrayList<>();
BufferedReader reader = null;
try {
if (charsetName == null) {
reader = new BufferedReader(new FileReader(file));
} else {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
}
String line;
while ((line = reader.readLine()) != null) {
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
closeIO(reader);
}
return list;
public static List<String> readFile2List(File file, String charsetName) {
return readFile2List(file, 0, 0x7FFFFFFF, charsetName);
}

/**
* 读取前几行数据
* 指定编码按行读取文件到List
*
* @param file 文件
* @param endLineNum 需要读取的行数
* @param filePath 文件路径
* @param start 需要读取的开始行数
* @param end 需要读取的结束行数
* @param charsetName 编码格式
* @return 包含制定行的list
*/
public static List<String> readFile2List(String filePath, int start, int end, String charsetName) {
return readFile2List(getFileByPath(filePath), start, end, charsetName);
}

/**
* 指定编码按行读取文件到List
*
* @param file 文件
* @param start 需要读取的开始行数
* @param end 需要读取的结束行数
* @param charsetName 编码格式
* @return 包含制定行的list
*/
public static List<String> readFileByLine(File file, int endLineNum) {
public static List<String> readFile2List(File file, int start, int end, String charsetName) {
if (file == null) return null;
List<String> list = new ArrayList<>();
if (start > end) return null;
List<String> list = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
int curLine = 1;
list = new ArrayList<>();
if (StringUtils.isSpace(charsetName)) {
reader = new BufferedReader(new FileReader(file));
} else {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
}
while ((line = reader.readLine()) != null) {
list.add(line);
if (list.size() == endLineNum) {
break;
}
if (curLine > end) break;
if (start <= curLine && curLine <= end) list.add(line);
++curLine;
}
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -624,73 +629,44 @@ public static List<String> readFileByLine(File file, int endLineNum) {
return list;
}

public static StringBuilder readFile(String filePath, String charsetName) {
File file = new File(filePath);
if (!file.isFile()) return null;
return readFile(file, charsetName);
/**
* 指定编码按行读取文件到StringBuilder中
*
* @param filePath 文件路径
* @param charsetName 编码格式
* @return StringBuilder对象
*/
public static StringBuilder readFile2SB(String filePath, String charsetName) {
return readFile2SB(getFileByPath(filePath), charsetName);
}

public static StringBuilder readFile(File file, String charsetName) {
StringBuilder sb = new StringBuilder("");
/**
* 指定编码按行读取文件到StringBuilder中
*
* @param file 文件
* @param charsetName 编码格式
* @return StringBuilder对象
*/
public static StringBuilder readFile2SB(File file, String charsetName) {
if (file == null) return null;
StringBuilder sb = null;
BufferedReader reader = null;
try {
InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
reader = new BufferedReader(is);
sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
String line;
while ((line = reader.readLine()) != null) {
if (!sb.toString().equals("")) {
sb.append("\r\n");
}
sb.append(line);
sb.append("\r\n");// windows系统换行为\r\n,Linux为\n
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
closeIO(reader);
}
return sb;
}


/**
* @param filePath
* @param charsetName
* @return
*/
public static List<String> readFileToList(String filePath, String charsetName) {
File file = new File(filePath);
if (!file.isFile()) return null;
List<String> fileContent = new ArrayList<>();
BufferedReader reader = null;
try {
InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
reader = new BufferedReader(is);
String line;
while ((line = reader.readLine()) != null) {
fileContent.add(line);
}
reader.close();
return fileContent;
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* byte单位转换(单位:unit)
*
Expand Down Expand Up @@ -750,23 +726,35 @@ public static double getFileSize(File file, int unit) {
}

/**
* 获取文件路径的父级目录
* 根据全路径获取最长目录
*
* @param filePath
* @return
* @param filePath 文件路径
* @return filePath最长目录
*/
public static String getDirName(String filePath) {
if (StringUtils.isSpace(filePath)) return filePath;
int lastSep = filePath.lastIndexOf(File.separator);
return lastSep == -1 ? "" : filePath.substring(0, lastSep + 1);
}

/**
* 根据全路径获取文件名
*
* @param filePath 文件路径
* @return 文件名
*/
public static String getFileName(String filePath) {
if (StringUtils.isSpace(filePath)) return filePath;
int lastSep = filePath.lastIndexOf(File.separator);
return lastSep == -1 ? filePath : filePath.substring(lastSep + 1);
}

/**
* 根据全路径获取文件名不带拓展名
*
* @param filePath 文件路径
* @return 文件名不带拓展名
*/
public static String getFileNameNoExtension(String filePath) {
if (StringUtils.isSpace(filePath)) return filePath;
int lastPoi = filePath.lastIndexOf('.');
Expand All @@ -780,6 +768,12 @@ public static String getFileNameNoExtension(String filePath) {
return filePath.substring(lastSep + 1, lastPoi);
}

/**
* 根据全路径获取文件拓展名
*
* @param filePath 文件路径
* @return 文件拓展名
*/
public static String getFileExtension(String filePath) {
if (StringUtils.isSpace(filePath)) return filePath;
int lastPoi = filePath.lastIndexOf('.');
Expand Down

0 comments on commit 5ae8ac2

Please sign in to comment.