
java里可以再在resources里面新建一个XML file或者 file文件
XML file 会自动生成XML头,在下面加入内容就可以了,首先要有一个根节点,然后如果需要用到一些类,如:spring的一些类,就需要引入包,如:
<xml version="10" encoding="UTF-8">
<beans xmlns=">
关于FileInputStream你的理解是对的,读取 *** 作是顺序向前,读过的不会再读。
不过第二个参数0你可能是误会了。它的作用不是指fis里的位置,而是指buffer里的偏移量。
在read中,0是指从buffer[0]开始保存数据;wirte中是指写出的数据是从buffer[0]开始。
Java Doc里的解释是这样的:
int javaioFileInputStreamread(byte[] b, int off, int len) throws IOException
b the buffer into which the data is read
off the start offset in the destination array b
len the maximum number of bytes read
看你这个txt文件内容详实xml,其实你可以吧txt改成xml文件,这样就能调用java的xml读取方法获得了,如果不能改文件类型,可以用java io读取每一行,然后定位height 和width的位置,位置加2(也就是=“的位置)这样就能获得下一位,即height和width里面的值了
获取文件类型,一般的是列出目前所有的文件类型,根据表头进行相应判断,示例如下:
/件头是位于文件开头的一段承担一定任务的数据,一般都在开头的部分。
头文件作为一种包含功能函数、数据接口声明的载体文件,用于保存程序的声明(declaration),而定义文件用于保存程序的实现 (implementation)。
为了解决在用户上传文件的时候在服务器端判断文件类型的问题,故用获取文件头的方式,直接读取文件的前几个字节,来判断上传文件是否符合格式。具体代码如下:
Java代码 :
/
package comyonyousudfile;
import javaioFileInputStream;
import javaioIOException;
import javautilHashMap;
/
获取和判断文件头信息
@author Sud
/
public class GetTypeByHead {
//缓存文件头信息-文件头信息
public static final HashMap<String, String> mFileTypes = new HashMap<String, String>();
static {
// images
mFileTypesput("FFD8FF", "jpg");
mFileTypesput("89504E47", "png");
mFileTypesput("47494638", "gif");
mFileTypesput("49492A00", "tif");
mFileTypesput("424D", "bmp");
//
mFileTypesput("41433130", "dwg"); // CAD
mFileTypesput("38425053", "psd");
mFileTypesput("7B5C727466", "rtf"); // 日记本
mFileTypesput("3C3F786D6C", "xml");
mFileTypesput("68746D6C3E", "html");
mFileTypesput("44656C69766572792D646174653A", "eml"); // 邮件
mFileTypesput("D0CF11E0", "doc");
mFileTypesput("5374616E64617264204A", "mdb");
mFileTypesput("252150532D41646F6265", "ps");
mFileTypesput("255044462D312E", "pdf");
mFileTypesput("504B0304", "docx");
mFileTypesput("52617221", "rar");
mFileTypesput("57415645", "wav");
mFileTypesput("41564920", "avi");
mFileTypesput("2E524D46", "rm");
mFileTypesput("000001BA", "mpg");
mFileTypesput("000001B3", "mpg");
mFileTypesput("6D6F6F76", "mov");
mFileTypesput("3026B2758E66CF11", "asf");
mFileTypesput("4D546864", "mid");
mFileTypesput("1F8B08", "gz");
}
/
根据文件路径获取文件头信息
@param filePath
文件路径
@return 文件头信息
/
public static String getFileType(String filePath){
Systemoutprintln(getFileHeader(filePath));
Systemoutprintln(mFileTypesget(getFileHeader(filePath)));
return mFileTypesget(getFileHeader(filePath));
}
/
根据文件路径获取文件头信息
@param filePath
文件路径
@return 文件头信息
/
public static String getFileHeader(String filePath){
FileInputStream is = null;
String value = null;
try {
is = new FileInputStream(filePath);
byte[] b = new byte[4];
/int read() 从此输入流中读取一个数据字节。
int read(byte[] b) 从此输入流中将最多 blength 个字节的数据读入一个 byte 数组中。
int read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
/
isread(b, 0, blength);
value = bytesToHexString(b);
} catch (Exception e){
} finally {
if (null != is){
try {
isclose();
} catch (IOException e){
}
}
}
return value;
}
/
将要读取文件头信息的文件的byte数组转换成string类型表示
@param src
要读取文件头信息的文件的byte数组
@return 文件头信息
/
private static String bytesToHexString(byte[] src){
StringBuilder builder = new StringBuilder();
if (src == null || srclength <= 0){
return null;
}
String hv;
for (int i = 0; i < srclength; i++){
// 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
hv = IntegertoHexString(src[i] & 0xFF)toUpperCase();
if (hvlength() < 2){
builderappend(0);
}
builderappend(hv);
}
Systemoutprintln(buildertoString());
return buildertoString();
}
public static void main(String[] args)throws Exception {
final String fileType = getFileType("E:/Java编程思想读书笔记docx");
Systemoutprintln(fileType);
}
}
ZipInputStream可以读取zip文件,构造的参数是FileInputStream等
|----getNextEntry() :可以得到zip文件中的一个子文件,再次调用得到下一个子文件
ZipEntry类中的getName():可以得到子文件的文件名
java读取文件方法大全
一、多种方式读文件内容。
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
Java代码
1 import javaioBufferedReader;
2 import javaioFile;
3 import javaioFileInputStream;
4 import javaioFileReader;
5 import javaioIOException;
6 import javaioInputStream;
7 import javaioInputStreamReader;
8 import javaioRandomAccessFile;
9 import javaioReader;
10
11 public class ReadFromFile {
12 /
13 以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。
14
15 @param fileName
16 文件的名
17 /
18 public static void readFileByBytes(String fileName) {
19 File file = new File(fileName);
20 InputStream in = null;
21 try {
22 Systemoutprintln("以字节为单位读取文件内容,一次读一个字节:");
23 // 一次读一个字节
24 in = new FileInputStream(file);
25 int tempbyte;
26 while ((tempbyte = inread()) != -1) {
27 Systemoutwrite(tempbyte);
28 }
29 inclose();
30 } catch (IOException e) {
31 eprintStackTrace();
32 return;
33 }
34 try {
35 Systemoutprintln("以字节为单位读取文件内容,一次读多个字节:");
36 // 一次读多个字节
37 byte[] tempbytes = new byte[100];
38 int byteread = 0;
39 in = new FileInputStream(fileName);
40 ReadFromFileshowAvailableBytes(in);
41 // 读入多个字节到字节数组中,byteread为一次读入的字节数
42 while ((byteread = inread(tempbytes)) != -1) {
43 Systemoutwrite(tempbytes, 0, byteread);
44 }
45 } catch (Exception e1) {
46 e1printStackTrace();
47 } finally {
48 if (in != null) {
49 try {
50 inclose();
51 } catch (IOException e1) {
52 }
53 }
54 }
55 }
56
57 /
58 以字符为单位读取文件,常用于读文本,数字等类型的文件
59
60 @param fileName
61 文件名
62 /
63 public static void readFileByChars(String fileName) {
64 File file = new File(fileName);
65 Reader reader = null;
66 try {
67 Systemoutprintln("以字符为单位读取文件内容,一次读一个字节:");
68 // 一次读一个字符
69 reader = new InputStreamReader(new FileInputStream(file));
70 int tempchar;
71 while ((tempchar = readerread()) != -1) {
72 // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
73 // 但如果这两个字符分开显示时,会换两次行。
74 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
75 if (((char) tempchar) != '\r') {
76 Systemoutprint((char) tempchar);
77 }
78 }
79 readerclose();
80 } catch (Exception e) {
81 eprintStackTrace();
82 }
83 try {
84 Systemoutprintln("以字符为单位读取文件内容,一次读多个字节:");
85 // 一次读多个字符
86 char[] tempchars = new char[30];
87 int charread = 0;
88 reader = new InputStreamReader(new FileInputStream(fileName));
89 // 读入多个字符到字符数组中,charread为一次读取字符数
90 while ((charread = readerread(tempchars)) != -1) {
91 // 同样屏蔽掉\r不显示
92 if ((charread == tempcharslength)
93 && (tempchars[tempcharslength - 1] != '\r')) {
94 Systemoutprint(tempchars);
95 } else {
96 for (int i = 0; i < charread; i++) {
97 if (tempchars[i] == '\r') {
98 continue;
99 } else {
100 Systemoutprint(tempchars[i]);
101 }
102 }
103 }
104 }
105
106 } catch (Exception e1) {
107 e1printStackTrace();
108 } finally {
109 if (reader != null) {
110 try {
111 readerclose();
112 } catch (IOException e1) {
113 }
114 }
115 }
116 }
117
118 /
119 以行为单位读取文件,常用于读面向行的格式化文件
120
121 @param fileName
122 文件名
123 /
124 public static void readFileByLines(String fileName) {
125 File file = new File(fileName);
126 BufferedReader reader = null;
127 try {
128 Systemoutprintln("以行为单位读取文件内容,一次读一整行:");
129 reader = new BufferedReader(new FileReader(file));
130 String tempString = null;
131 int line = 1;
132 // 一次读入一行,直到读入null为文件结束
133 while ((tempString = readerreadLine()) != null) {
134 // 显示行号
135 Systemoutprintln("line " + line + ": " + tempString);
136 line++;
137 }
138 readerclose();
139 } catch (IOException e) {
140 eprintStackTrace();
141 } finally {
142 if (reader != null) {
143 try {
144 readerclose();
145 } catch (IOException e1) {
146 }
147 }
148 }
149 }
150
151 /
152 随机读取文件内容
153
154 @param fileName
155 文件名
156 /
157 public static void readFileByRandomAccess(String fileName) {
158 RandomAccessFile randomFile = null;
159 try {
160 Systemoutprintln("随机读取一段文件内容:");
161 // 打开一个随机访问文件流,按只读方式
162 randomFile = new RandomAccessFile(fileName, "r");
163 // 文件长度,字节数
164 long fileLength = randomFilelength();
165 // 读文件的起始位置
166 int beginIndex = (fileLength > 4) 4 : 0;
167 // 将读文件的开始位置移到beginIndex位置。
168 randomFileseek(beginIndex);
169 byte[] bytes = new byte[10];
170 int byteread = 0;
171 // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
172 // 将一次读取的字节数赋给byteread
173 while ((byteread = randomFileread(bytes)) != -1) {
174 Systemoutwrite(bytes, 0, byteread);
175 }
176 } catch (IOException e) {
177 eprintStackTrace();
178 } finally {
179 if (randomFile != null) {
180 try {
181 randomFileclose();
182 } catch (IOException e1) {
183 }
184 }
185 }
186 }
187
188 /
189 显示输入流中还剩的字节数
190
191 @param in
192 /
193 private static void showAvailableBytes(InputStream in) {
194 try {
195 Systemoutprintln("当前字节输入流中的字节数为:" + inavailable());
196 } catch (IOException e) {
197 eprintStackTrace();
198 }
199 }
200
201 public static void main(String[] args) {
202 String fileName = "C:/temp/newTemptxt";
203 ReadFromFilereadFileByBytes(fileName);
204 ReadFromFilereadFileByChars(fileName);
205 ReadFromFilereadFileByLines(fileName);
206 ReadFromFilereadFileByRandomAccess(fileName);
207 }
208 }
二、将内容追加到文件尾部
1 import javaioFileWriter;
2 import javaioIOException;
3 import javaioRandomAccessFile;
4
5 /
6 将内容追加到文件尾部
7 /
8 public class AppendToFile {
9
10 /
11 A方法追加文件:使用RandomAccessFile
12 @param fileName 文件名
13 @param content 追加的内容
14 /
15 public static void appendMethodA(String fileName, String content) {
16 try {
17 // 打开一个随机访问文件流,按读写方式
18 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
19 // 文件长度,字节数
20 long fileLength = randomFilelength();
21 //将写文件指针移到文件尾。
22 randomFileseek(fileLength);
23 randomFilewriteBytes(content);
24 randomFileclose();
25 } catch (IOException e) {
26 eprintStackTrace();
27 }
28 }
29
30 /
31 B方法追加文件:使用FileWriter
32 @param fileName
33 @param content
34 /
35 public static void appendMethodB(String fileName, String content) {
36 try {
37 //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
38 FileWriter writer = new FileWriter(fileName, true);
39 writerwrite(content);
40 writerclose();
41 } catch (IOException e) {
42 eprintStackTrace();
43 }
44 }
45
46 public static void main(String[] args) {
47 String fileName = "C:/temp/newTemptxt";
48 String content = "new append!";
49 //按方法A追加文件
50 AppendToFileappendMethodA(fileName, content);
51 AppendToFileappendMethodA(fileName, "append end \n");
52 //显示文件内容
53 ReadFromFilereadFileByLines(fileName);
54 //按方法B追加文件
55 AppendToFileappendMethodB(fileName, content);
56 AppendToFileappendMethodB(fileName, "append end \n");
57 //显示文件内容
58 ReadFromFilereadFileByLines(fileName);
59 }
60 }
可以看到,man(String[] args):所以args是属于数组,所以设置类型为数组就好:
argstest(String[] arr),调用的时候同样传入数组args
望采纳
以上就是关于Java里如何添加自定义的配置文件,JSP里去读取参数全部的内容,包括:Java里如何添加自定义的配置文件,JSP里去读取参数、java中获取文件内容中的所有数字,要求如下: 1.创建sadk.txt文件(文件输入可手动创建和录入);、java读取文件 *** 作等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)