java 中.get显示文件后缀类型用什么函数
时间: 2024-03-08 08:51:24 浏览: 21
在 Java 中,可以使用 `java.nio.file.Path` 类的 `getFileName` 方法来获取文件的名称,然后再使用 `java.lang.String` 类的 `endsWith` 方法来判断文件名是否以特定的后缀名结尾。例如,以下代码可以判断文件路径 `filePath` 是否以 `.txt` 结尾:
```java
Path path = Paths.get(filePath);
String fileName = path.getFileName().toString();
if (fileName.endsWith(".txt")) {
System.out.println("This is a text file.");
}
```
相关问题
使用java书写一个函数,他的功能是中缀表达式转后缀表达式,并且计算最后结果,包含小数
好的,以下是一个使用Java编写的函数,实现中缀表达式转后缀表达式并计算最后结果,包含小数的情况:
```java
import java.util.*;
public class InfixToPostfixCalculator {
public static double calculate(String expression) {
// 定义运算符的优先级
Map<Character, Integer> precedence = new HashMap<>();
precedence.put('+', 1);
precedence.put('-', 1);
precedence.put('*', 2);
precedence.put('/', 2);
List<String> postfix = new ArrayList<>(); // 存储后缀表达式
Stack<Character> stack = new Stack<>(); // 运算符栈
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (Character.isDigit(c) || c == '.') {
StringBuilder num = new StringBuilder();
while (i < expression.length() && (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
num.append(expression.charAt(i));
i++;
}
postfix.add(num.toString());
i--;
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
postfix.add(stack.pop().toString());
}
stack.pop(); // 弹出左括号
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
while (!stack.isEmpty() && stack.peek() != '(' && precedence.get(c) <= precedence.get(stack.peek())) {
postfix.add(stack.pop().toString());
}
stack.push(c);
}
}
while (!stack.isEmpty()) {
postfix.add(stack.pop().toString());
}
Stack<Double> operandStack = new Stack<>();
for (String token : postfix) {
if (token.matches("\\d+(\\.\\d+)?")) {
operandStack.push(Double.parseDouble(token));
} else {
double num2 = operandStack.pop();
double num1 = operandStack.pop();
if (token.equals("+")) {
operandStack.push(num1 + num2);
} else if (token.equals("-")) {
operandStack.push(num1 - num2);
} else if (token.equals("*")) {
operandStack.push(num1 * num2);
} else if (token.equals("/")) {
operandStack.push(num1 / num2);
}
}
}
return operandStack.pop();
}
}
```
您可以调用 `calculate` 方法,并将中缀表达式作为参数传入。方法将返回计算结果。例如:
```java
String expression = "3 + 4 * 2 / ( 1 - 5 )";
double result = InfixToPostfixCalculator.calculate(expression);
System.out.println(result); // 输出结果为 1.0
```
请注意,以上代码只支持单个数字的计算,如果需要支持多位数的计算,需要对输入表达式进行解析和拼接。
键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数 注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,并用两种方式遍历map集合(iterator和entry) 例如: doc 的类型的文件有 3 个 java 的类型的文件有 5 个 txt 的类型的文件有 7 个 进一步思考: 如果要求 对目录下面所有的子目录也进行统计,如何实现?
以下是Python代码实现:
```python
import os
def count_files(path, result):
"""
统计指定路径下文件类型及个数
:param path: 指定路径
:param result: 存储结果的字典
"""
for root, dirs, files in os.walk(path):
for file in files:
# 获取文件类型
ext = os.path.splitext(file)[-1][1:]
if ext:
result[ext] = result.get(ext, 0) + 1
if __name__ == '__main__':
path = input("请输入文件夹路径:")
result = {}
count_files(path, result)
print("文件类型及个数:")
# iterator遍历
for key in result:
print(key + " 的类型的文件有 " + str(result[key]) + " 个")
# entry遍历
print("文件类型及个数:")
for item in result.items():
print(item[0] + " 的类型的文件有 " + str(item[1]) + " 个")
```
如果要对目录下面所有的子目录也进行统计,可以将count_files函数中的os.walk改为os.listdir,并在函数内部判断是否为目录,如果是目录则递归调用count_files函数即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)