关于javafileio的信息
本篇文章给大家谈谈javafileio,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、求教一个java io程序
- 2、用java读取桌面上的文档"abc.txt",程序怎么写?
- 3、java中文件读取的时候,要被读取的文件应该放哪
- 4、java如何高效的读取超长字符串
- 5、求问一个java fileio流的小问题
- 6、两道Java题目,具体如下
求教一个java io程序
//TestIO.java
import java.io.*;
import java.util.*;
public class TestIO {
public static void main(String[] args){
//定义一些变量
BufferedReader br = null;
FileIOMethods fm = null;
File f = null;
byte[] b = null;
String fStr = "";
StringBuffer sb = new StringBuffer();
br = new BufferedReader(new InputStreamReader(System.in));//输入流,用以获取键盘输入,并写入文件
f = new File("1.txt");//要用到的文件
if(!f.exists()){//如果文件不存在,就创建它
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("请输入信息:");
try {//读取键盘输入
fStr = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fm = new FileIOMethods();//new一个对象
fm.MyFileWriter(fStr, f);//调用方法将读取的键盘输入信息写入文件
sb = fm.MyFileReader(f);//调用方法从文件中读取数据
if(sb != null !sb.equals("")){
System.out.println(sb.toString());
}else{
System.out.println("文件为空!");
}
}
}
class FileIOMethods{//自定义的读取输入和打印输出的类
/**
* 获取一个字符串,将其写入文件中
*/
public void MyFileWriter(String msg,File f){
BufferedWriter bw = null;
if(!f.exists()){//若文件不存在,返回
System.out.println(f.getName() + "不存在!");
return;
}
try {
bw = new BufferedWriter(new FileWriter(f));
if(msg != null !msg.equals("")){
bw = new BufferedWriter(new FileWriter(f));//打开流
bw.write(msg + " " + new Date().toLocaleString());//将数据写入文件,并在数据尾部加上当前时间
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bw.flush();//清空流
bw.close();//关闭流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 从文件中读取信息,存入StringBuffer中并返回
* */
public StringBuffer MyFileReader(File f){
BufferedReader br = null;
String msg = "";
StringBuffer sb = null;
if(!f.exists()){//若文件不存在,返回
System.out.println(f.getName() + "不存在!");
}
try {
sb = new StringBuffer();
br = new BufferedReader(new FileReader(f));//打开流
while(true){
try {
msg = br.readLine();//从文件读取数据
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(msg == null) break;//若数据为空,则跳出循环
sb.append(msg + "\n");//将获取的数据放入StringBuffer中
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br != null){
try {//关闭流
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb;
}
}
}
用java读取桌面上的文档"abc.txt",程序怎么写?
给一个代码你就知道了,代码里面文件的路径改成你电脑上的文件的路径
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* file IO流读取并输出文件
* @author Administrator
*
*/
public class FileIO {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("src/day03/BrDemo.java");// 要读的文件路径
InputStreamReader isr = new InputStreamReader(fis);// 字符流
BufferedReader br = new BufferedReader(isr); // 缓冲
String line = null;
while ((line = br.readLine()) != null) {// 字符不等于空
System.out.println(line);// 一行一行地输出
}
br.close();// 关闭文件
}
}
java中文件读取的时候,要被读取的文件应该放哪
被读取的文件可以放在硬盘的任意位置。 只要你新建文件IO流对象的时候把文件的物理路径写对就行了。代码例子如下:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* file IO流读取并输出文件
* @author young
*
*/
public class FileIO {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("F:/workspace/one/src/filecode/FileIO.java");// 要读的文件路径
InputStreamReader isr = new InputStreamReader(fis);// 字符流
BufferedReader br = new BufferedReader(isr); // 缓冲
String line = null;
while ((line = br.readLine()) != null) {// 字符不等于空
System.out.println(line);// 一行一行地输出
}
br.close();// 关闭文件
}
}
java如何高效的读取超长字符串
案例:通过测试是最快的了一种
普通IO耗时1.3s
public static String openStringFileIO(String path, String fileName) {
long time = System.currentTimeMillis();
String result = null;
File f = new File(path, fileName);
try {
FileInputStream fileInputStream = new FileInputStream(f);
StringBuilder buffer = new StringBuilder();
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(fileInputStream));
while ((line = in.readLine()) != null) {
buffer.append(line);
}
result = buffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
Logger.d("openStringFileIO " + (System.currentTimeMillis() - time));
return result;
}
求问一个java fileio流的小问题
FileInputStream.read(byte[])是一次性读完的 返回的是已读取的字节数
new String(bytes,0,n) 你用n把后面的0b数据全切掉了
不用的话 1024byte全读了,所以会有意外的信息
两道Java题目,具体如下
//第一题:
import java.util.*;
//编写一个函数,用于生成随机密码,入参为随机密码的长度,返回生成的随机密码.要求生成的随机密码必须同时包含大写字母,小写字母和数字.
public class B1 {
public static void main(String[] args){
System.out.println(getPassword(9)); //调用getPassword方法,然后输出打印返回的密码
}
public static String getPassword(int length){
ArrayList al = new ArrayList(); //定义一个集合存储a-z和A-Z还有0-9
String password = "";
for(int x = 'a';x 'a'+26;x++){ //存入a-z
al.add((char)x);
}
for(int x = 'A';x 'A'+26;x++){ //存入A-Z
al.add((char)x);
}
for(int x = 0;x = 9;x++){ //存入0-9
al.add(x);
}
for(int x = 0;xlength;x++){ //按照传递进来的长度循环随机
int n = (int)(Math.random()*al.size()); //随机数
password += al.get(n); //获取随机到的密码
}
return password; //返回随机好的密码
}
}
//第二题:
import java.io.*;
//编写一个函数,实现从磁盘读取一个文本文件,将内容逐行输出.注意需要包含异常处理代码
public class B2 {
public static void main(String[] args){
printFile(new File("d:\\io.txt"));
}
public static void printFile(File file){
BufferedReader bfr = null;
try{
bfr = new BufferedReader(new FileReader(file));
String value = null;
while((value = bfr.readLine())!=null){
System.out.println(value);
}
}
catch(IOException e){
System.out.println("文件读取失败!");
}
finally{
try{
if(bfr != null)
bfr.close();
}
catch(IOException e){
System.out.println("读取流关闭失败!");
}
}
}
}
javafileio的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javafileio的信息别忘了在本站进行查找喔。
发布于:2022-11-25,除非注明,否则均为
原创文章,转载请注明出处。