实现统计个人某个目录下的java 文件个数,总代码行数。
package j09;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Tongh {
public static void main(String[] args) throws IOException {
String str = "E:/java/";
tongji(str);
System.out.printf("java文件%d共%d行代码",a,d);
}
public static void tongji(String str) {
try {
tongji(new File(str));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static int a = 0, s = 0, d = 0;
public static void tongji(File file) throws IOException {
if (file.isDirectory()) {
File[] ile = file.listFiles();
for (File il : ile) {
tongji(il);
}
} else {
String name = file.getName();String kuo="";
if(name.lastIndexOf(".")==-1) {}else {
kuo =name.substring(name.lastIndexOf("."));}
if (kuo. contains(".java")) {
++a;
FileReader read = new FileReader(file);
BufferedReader fil = new BufferedReader(read);
s=0;
while (fil.ready()) {
fil.readLine();
++s;
}
d += s;
System.out.println(file.getName()+s+"行");
}
}
}
}
自动加水印
package j09;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Tu {
public static void main(String[] args) {
try {
shui();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void shui() throws IOException {
String str="C:\\Users\\Administrator\\Pictures\\error.png";
File file=new File(str);
shui(file);
}
public static void shui(File file) throws IOException {
String dst=file.getPath();
BufferedImage s=ImageIO.read(file);
int width =s.getWidth();
int height= s.getHeight();
BufferedImage i=new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
Graphics g=i.getGraphics();
g.drawImage(s, 0, 0, width,height,null);
g.setColor(new Color(0,0,122,50));
g.fillRect(0, 0, width, height);
g.setColor(new Color(255,32,122,120));
g.setFont(new Font("黑体",Font.BOLD,40));
int x=width-200;
int y=height-35;
g.drawString("你好哈", x, y);
g.dispose();
ImageIO.write(i, "jpg", new File(dst));
}
}

浙公网安备 33010602011771号