
LZ可以在控制台输入参数并且读取,按照LZ的要求,有注释
import javaioBufferedInputStream;
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioIOException;
import javautilScanner;
public class ReadConsole {
public static void main(String[] args) {
Scanner sc = new Scanner(Systemin);//接受控制台输入的路径
Systemoutprintln("请输入您要读取的文件位置:");
String path = scnext();//获得输入的字符串
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));//字节方式读取
byte[] bytes = new byte[200];//设置缓冲区大小
inread(bytes);//读取
Systemoutprintln(new String(bytes));//打印读入的字符
} catch (FileNotFoundException e) {
Systemoutprintln("对不起,您输入的路径不存在");
} catch (IOException e) {
Systemoutprintln("对不起,读取错误");
}
}
}
import javaioBufferedReader;
import javaioFileNotFoundException;
import javaioFileReader;
import javaioIOException;
public class Test2 {
public static void main(String args[]) {
int x1, x2, x3, x4;
String string1;
try {
BufferedReader br;
br = new BufferedReader(new FileReader("E:\\bjbat"));
String lineInfo, lineWords[];
lineInfo = brreadLine();
lineWords = lineInfosplit(" ");
x1 = IntegerparseInt(lineWords[0]);
x2 = IntegerparseInt(lineWords[1]);
x3 = IntegerparseInt(lineWords[2]);
x4 = IntegerparseInt(lineWords[3]);
string1 = lineWords[4];
Systemoutprintln("x1="+x1+",x2="+x2+",x3="+x3+",x4="+x4+",string1="+string1);
} catch (FileNotFoundException e) {eprintStackTrace();
} catch (IOException e) {eprintStackTrace();}
}
}
package domemywordtest;
import javaioBufferedInputStream;
import javaioBufferedReader;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioFileReader;
import javaioIOException;
public class Myword {
public static void main(String[] args) throws IOException {
try {
FileInputStream file = new FileInputStream("e:/myTexttxt");
BufferedInputStream Bfile = new BufferedInputStream(file);
byte[] b = new byte[1024];
String s = "";
int bytesRead=0;
while((bytesRead=Bfileread(b))!=-1){
s+= new String(b,0,bytesRead);
}
Systemoutprintln(s);
String[] words = ssplit(" ");
int sum = wordslength;
int max = words[0]length();
String maxWord = "";
int avgs = 0;
for (int i = 1; i < wordslength; i++) {
if(words[i]length()>max) {
max = Mathmax(max, words[i]length());
maxWord = words[i];
}
avgs = avgs+words[i]length()+words[0]length();
}
String path = "e:/myTexttxt" ;
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
int x = 0;
while(brreadLine() != null) {
x++;
}
Systemoutprintln("总行数"+x);
int avg = avgs/sum;
Systemoutprintln("平均长度:"+avg);
Systemoutprintln("最长单词:"+maxWord);
Systemoutprintln("单词总数:"+sum);
Bfileclose();
String fileName = "e:/mytextdatatxt";
FileOutputStream out = new FileOutputStream(fileName);
String str ="单词总数:"+sum+"\r\n"+ "总行数:"+x+"\r\n"+"最长单词:"+maxWord+"\r\n"+"平均长度:"+avg;
outwrite(strgetBytes());
outclose();
Systemoutprintln("输出文本完毕");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
有问题再叫我。下面是测试结果
while(line != null)
{
Systemoutprintln(line);
outputwrite(line);
outputnewLine();
line = brreadLine();
}
outputflush();
outputclose();
如果没有flush,写入的数据可能不完整。循环完以后加上这两句即可
public class ReadFromFile {
/
以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。
/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
Systemoutprintln("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = inread()) != -1) {
Systemoutwrite(tempbyte);
}
inclose();
} catch (IOException e) {
eprintStackTrace();
return;
}
try {
Systemoutprintln("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFileshowAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = inread(tempbytes)) != -1) {
Systemoutwrite(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1printStackTrace();
} finally {
if (in != null) {
try {
inclose();
} catch (IOException e1) {
}
}
}
}
/
以字符为单位读取文件,常用于读文本,数字等类型的文件
/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
Systemoutprintln("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = readerread()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
Systemoutprint((char) tempchar);
}
}
readerclose();
} catch (Exception e) {
eprintStackTrace();
}
try {
Systemoutprintln("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = readerread(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempcharslength)
&& (tempchars[tempcharslength - 1] != '\r')) {
Systemoutprint(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
Systemoutprint(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1printStackTrace();
} finally {
if (reader != null) {
try {
readerclose();
} catch (IOException e1) {
}
}
}
}
/
以行为单位读取文件,常用于读面向行的格式化文件
/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
Systemoutprintln("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = readerreadLine()) != null) {
// 显示行号
Systemoutprintln("line " + line + ": " + tempString);
line++;
}
readerclose();
} catch (IOException e) {
eprintStackTrace();
} finally {
if (reader != null) {
try {
readerclose();
} catch (IOException e1) {
}
}
}
}
/
随机读取文件内容
/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
Systemoutprintln("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFilelength();
// 读文件的起始位置
int beginIndex = (fileLength > 4) 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
randomFileseek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFileread(bytes)) != -1) {
Systemoutwrite(bytes, 0, byteread);
}
} catch (IOException e) {
eprintStackTrace();
} finally {
if (randomFile != null) {
try {
randomFileclose();
} catch (IOException e1) {
}
}
}
}
/
显示输入流中还剩的字节数
/
private static void showAvailableBytes(InputStream in) {
try {
Systemoutprintln("当前字节输入流中的字节数为:" + inavailable());
} catch (IOException e) {
eprintStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemptxt";
ReadFromFilereadFileByBytes(fileName);
ReadFromFilereadFileByChars(fileName);
ReadFromFilereadFileByLines(fileName);
ReadFromFilereadFileByRandomAccess(fileName);
}
}
复制代码
5、将内容追加到文件尾部
public class AppendToFile {
/
A方法追加文件:使用RandomAccessFile
/
public static void appendMethodA(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFilelength();
//将写文件指针移到文件尾。
randomFileseek(fileLength);
randomFilewriteBytes(content);
randomFileclose();
} catch (IOException e) {
eprintStackTrace();
}
}
/
B方法追加文件:使用FileWriter
/
public static void appendMethodB(String fileName, String content) {
try {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writerwrite(content);
writerclose();
} catch (IOException e) {
eprintStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemptxt";
String content = "new append!";
//按方法A追加文件
AppendToFileappendMethodA(fileName, content);
AppendToFileappendMethodA(fileName, "append end \n");
//显示文件内容
ReadFromFilereadFileByLines(fileName);
//按方法B追加文件
AppendToFileappendMethodB(fileName, content);
AppendToFileappendMethodB(fileName, "append end \n");
//显示文件内容
ReadFromFilereadFileByLines(fileName);
}
}
package file;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioIOException;
import javaioInputStream;
public class ReadFile {
/
@param args
@throws IOException
/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//将一个已存在文件加载到内存中,读取文本文件
InputStream is = new FileInputStream("xxxtxt");
//获得文件可读取的字节数
int num = isavailable();
Systemoutprintln(num);
char[] cs = new char[num];
for (int i = 0; i < num; i++) {
int num2 = isread();//ashc码
char c = (char)num2;//ashc转换成字符
cs[i]=c;
}
String str = new String(cs);
Systemoutprintln(str);
isclose();
}
}
嘎嘎嘎嘎,。。
package Test09;
import javaioBufferedInputStream;
import javaioBufferedReader;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStreamReader;
import javaioOutputStream;
import javaioOutputStreamWriter;
import javaioPrintWriter;
import javautilDate;
public class TextFileIODemo {
public static void main(String[] args) throws IOException {
OutputStream out = new FileOutputStream("txttxt");
OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
PrintWriter pw = new PrintWriter(osw);
pwprintln("HI, Tom");
pwprintln("好久不见, 你在哪里");
pwprintln("祝你顺安!");
pwprintln("Jerry");
pwprintln(new Date());// 值构造一个 Date 对象。
pwclose();
Systemoutprintln(IOUtilsreadAsHex("txttxt"));
byte[] utf8 = IOUtilsreadAll("txttxt");
String str = new String(utf8, "utf-8");
Systemoutprintln(str);
// 常见文本文件读取 *** 作
BufferedReader in = new BufferedReader(new InputStreamReader(
new BufferedInputStream(new FileInputStream("txttxt")),
"utf-8"));
String s;// //inreadLine()每次读取一行,到文件末尾返回null
while ((s = inreadLine()) != null) {
Systemoutprintln(s);
}
inclose();
}
}
以上就是关于JAVA 求一个java读取TXT文件的方法。全部的内容,包括:JAVA 求一个java读取TXT文件的方法。、java文件读取!、java,编写一个程序,可以读取文件数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)