需引入guava依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
public static void main(String[] args) {
try {
// 读取原始文件
FileInputStream fileInputStream = new FileInputStream("input.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
// 创建输出文件
FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
// 逐行处理
String line;
while ((line = bufferedReader.readLine()) != null) {
String underscoredLine = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, line);
// 写入转换后的行
bufferedWriter.write(underscoredLine);
bufferedWriter.newLine();
}
// 关闭文件流
bufferedReader.close();
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
以下其它案例:
public static void main(String[] args) {
System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "user-name"));//userName
System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_UNDERSCORE, "user-name"));//user_name
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "user_name"));//userName
System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "user_name"));//UserName
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, "userName"));//UserName
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "userName"));//user_name
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "UserName"));//user_name
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "userName"));//user-name
}