Eclipse 普通 Java Project 文件路径问题
博客园链接
项目的结构如图
读取 src 里某个包下的文件,代码如下
BufferedReader br=new BufferedReader(new FileReader("src/ch01/practice1/input.txt"));
输出到同一目录下,代码如下
PrintWriter pw=new PrintWriter("src/ch01/practice1/output.txt");
整个算法代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | package ch01.practice1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
//import java.util.Arrays;
//统计数字问题
public class PageNuber
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new FileReader("src/ch01/practice1/input.txt"));
String line=br.readLine();
if (null!=line)
{
int n=Integer.valueOf(line);
int[] arr= new int[10];
// Arrays.fill(arr,0);
// System.out.println(Arrays.toString(arr));
for (int i = 1; i < n+1; i++)
{
int div=i;
while(div>0)
{
arr[div%10]++;
div=div/10;
}
}
// System.out.println(Arrays.toString(arr));
PrintWriter pw=new PrintWriter("src/ch01/practice1/output.txt");
for (int i = 0; i < arr.length; i++)
{
pw.print(arr[i]+" ");
}
pw.close();
}
br.close();
}
}
|