java中读取源代码文件 java读取文件的代码

JAVA考查作业: 读取指定路径的源程序代码文件,打开文件,并将之读入内存,统计该程序的相关信息:你的问题真多 。。。。。。。。随便写一点吧,不全的你再查查
// 打开文件,并将之读入内存
public static String readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
String allString = null;
int line = 1;
// 一次读入一行 , 直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
allString = allString + tempString;
line++;
}
reader.close();
return allString ;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
---------------------------------------------------
1:大写字母的个数 。
2:小写字母的个数 。
4:数字的个数 。
首先利用上面的方法取得文件内容,返回值保存到String a中
int countNum = 0;
int countA = 0;
int counta = 0;
for(int i = 0;ia.length();i++){
if((byte)a.charAt(i)47(byte)a.charAt(i)58){
countNum++;
}else if((byte)a.charAt(i)64(byte)a.charAt(i)91){
countA++;
}else if((byte)a.charAt(i)96(byte)a.charAt(i)123){
counta++;
}
}
-----------------------------------------------------------------------
3:特殊字符的个数 。
不知道什么是特殊字符?
-----------------------------------------------------------------------
5:能将代码全部转化为大写字母并写入文件 。
6:能将代码全部转化为小写字母并写入文件 。
int max = a.length();
char[] anArray = new char[max];
for (int i = 0; imax; i++) // 将字符串一次存入字符数组中
{
anArray[i] = a.charAt(i);
}
for (int j = 0; janArray.length; j++) // 完成大小写转换
{
if (anArray[j] == ' ') {
continue;
} else if (anArray[j] = 'Z'anArray[j] = 'A') {
anArray[j] = (char) ((int) anArray[j] + 32);
} else {
anArray[j] = (char) ((int) anArray[j] - 32);
}
}
用FileInputStream类,实现从磁盘读取本应用程序源代码文件(在Java中)File f =new File("地址");
FileInputStream in = new FileInputStream(file);
Scanner reader =new Scanner(in);
//这是属于util包里的 比较好用也可以不用 直接用FileInputStream 读取
while(reader.haiNextLine()){
String str = reader.nextLine();
]
java程序读取一个url页面的源代码传入一个url,返回源代码; public static String getHTML(String url){// 获取指定URLjava中读取源代码文件的网页,返回网页内容java中读取源代码文件的字符串,然后将此字符串存到文件即可 try { URL newUrl = new URL(url); URLConnection connect = newUrl.openConnection(); connect.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); DataInputStream dis = new DataInputStream(connect.getInputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(dis,"UTF-8")); String html = ""; String readLine = null; while((readLine = in.readLine()) != null) { html = html + readLine; } in.close(); return html; }catch (MalformedURLException me){ System.out.println("MalformedURLException" + me); }catch (IOException ioe){ System.out.println("ioeException" + ioe); } return null; }
【java中读取源代码文件 java读取文件的代码】关于java中读取源代码文件和java读取文件的代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读