inputstream写入文件(inputstreamread)
求一个倒计时,秒表。需要JAVA. 需要有断电记忆功能
countdown(60);以下是一个简单的倒计时和秒表程序的JAVA代码示例,它包括断电记忆功能,将倒计时和秒表的时间保存到文件中,下次打开程序时从文件中读取并继续计时或计数。
inputstream写入文件(inputstreamread)
inputstream写入文件(inputstreamread)
import ja.io.;
public class TimerAndStopwatch {
private static final String TIMER_FILE = "timer.txt";
private static final String STOPWATCH_FILE = "stopwatch.txt";
public static void countdown(int seconds) throws InterruptedException {
// 读取上次保存的时间
int prevTime = readFromFile(TIMER_FILE);
if (prevTime > 0) {
seconds = prevTime;
}System.out.println("倒计时开始:" + seconds + " 秒");
while (seconds > 0) {
Thread.sleep(1000);
seconds--;
System.out.println("剩余时间:" + seconds + " 秒");
}System.out.println("时间到!");
}// 秒表
public static void stopwatch() throws InterruptedException {
// 读取上次保存的时间
int prevTime = readFromFile(STOPWATCH_FILE);
System.out.println("秒表开始!");
int seconds = prevTime;
while (true) {
Thread.sleep(1000);
seconds++;
System.out.println("计时:" + seconds + " 秒");
seToFile(STOPWATCH_FILE, seconds);
// 从文件中读取时间
private static int readFromFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line = reader.readLine();
if (line != null && !line.isEmpty()) {
return Integer.parseInt(line);
}} catch (IOException e) {
System.err.println("读取文件出错:" + e.getMessage());
}return 0;
}// 将时间保存到文件
private static void seToFile(String filename, int seconds) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
wri/ter.println(seconds);
} catch (IOException e) {
System.err.println("保存文件出错:" + e.getMessage());
public static void main(String[] args) throws InterruptedException {
stopwatch();
在这个程序中,我们使用了两个常量来表示保存时间的文件名,TIMER_FILE表示倒计时的文件名,STOPWATCH_FILE表示秒表的文件名。在倒计时和秒表开始时,我们读取上次保存的时间,如果有的话,将其作为初始时间;否则,使用默认时间。在计时或计数时,我们使用Thread.sleep(1000)让程序暂停1秒钟,然后更新时间或计数并保存到文件中。当程序下一次启动时,会从文件中读取保存的时间或计数,然后继续计时或计数。
编写ja程序将10个整数写入一个文件中,然后再从该文件中将10个数读出并显示 求教导!
e.printStackTrace();import ja.io.;
{try {public class Demo1 {
public static void main(String[] args) {
FileOutputStream fos=null;
FileInputStream fis=null;
File f=new File("E:a.txt");
//判断文件是否存在
if(!f.exists())
f.createNewFile();//如果文件不存在则创建此文件
} catch (IOException e) {
// TODO Auto-generated catch block
}}else{
System.out .println("此文件已经存在了");
}InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
fos=new FileOutputStream(f);
String s=br.readLine();//从键盘输入数据
fos.write(s.getBytes());//将数据写入到文件里
fis=new FileInputStream(f);
//定义一个字节数组,相当于缓存
byte bytes[]=new byte[1024];
int n=0;//实际读取到的字节数
//循环读取
while((n=fis.read(bytes))!=-1)
{//把字节转成string
String s1=new String(bytes, 0, n);
System.out.println(s1);//显示文件内容
}} catch (IOException e) {
// TODO Auto-generated catch block
}
jaIO:写入文件的内容少了
你只读了1024字节,如果文件大于1024字节你剩下的就没进行读写。
byte[] b = new byte[1024];//1
File BufferedWriter bw=new BufferedWriter(fw);file1 = new File(sourceFile);
File file2 = new File(copyFile);
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
i}这个只适用于文本文档,不能写word,excel,pdfnt i;
while(fis.read(b)>-1){
fos.write(b);
}
Ja中从文件中读取数据用什么函数
给你个例子:
import ja.io.;
public class TestFileReader {
public static void main(String[] args) {
FileReader fr = null;
int c = 0;
fr = new FileReader("d:sharejaioTestFileReader.ja");
int ln = 0;
while ((c = fr.read()) != -1) {
//char ch =bw.newLine(); (char) fr.read();
System.out.print((char)c);
}fr.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
System.out.println("文件读取错误");
//if (++ln >= 100) { System.out.println(); ln = 0;}}
跪求:ja读取二进制文件写成十六进制字符串,并把字符串写入txt。我的程序生成了,但却是空的。求解~~~
你输入流读进来存在内存中了,但是没往输出流里写啊
FileOutputStream fout = new FileOutpuimport ja.io.BufferedWriter;tStream("c:b.txt");//将十六进制数存成文本
fout.clospublic static void rwFile(){e();
new了一个输出流,然后就关了,加上fout.write(asciiData .getBytes()) 再关吧。
JAVA根据文件后缀名分类文件,并且将文件到不同的文件夹,求这段代码的注释
seToFile(TIMER_FILE, seconds);以下都是one by one letter打的,不容易,给点分吧。
import ja.io.File;
import ja.io.FileInputStream;
import ja.io.FileOutputStream;
import jax.swing.JFileChooser;
1.选择被的源目录:sourcePath
2.选择文件被后存放的目标目录:targetPath
3.文件后缀名分类文件,并且将文件到不同的文件夹
/
public class FileDemo {
启动方法
@param args
public static void main(String[] args) {
选择被的源目录:sourcePath
File sourcePath = getSourcePath();
选择文件被后存放的目标目录:targetPath
File outputPath = getOutputPath();
执行作
handlePath(sourcePath, outputPath);
}/
处理文件分类
@param sourcePath
@param outputPath
private static void handlePath(File sourcePath, File outputPath) {
if (sourcePath == null || outputPath == null) //1.如果没有选择源目录或目标目录,则返回
return;
for (File file : sourcePath.listFiles()) { //2.取得源目录下所有的文件或文件夹,并进行遍历
if (file.isDirectory()) {
//3.如果当前遍历的是文件夹,则使用递归调用,继续处理该文件夹下面的所有文件或文件夹
handlePath(file, outputPath);
} else {
//4.如果当前遍历的是不是文件夹而是文件,那么直接开始copy的作
//4.1.取得文件的文件名,包含文件后缀名
String fileName = file.getName();
if (fileName.contains(".")) {
//4.2.1.如果该文件有后缀名,即包含“.”点符号,则取得文件的后缀名为:一个点符号后面的字符串
String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
//4.2.2.根据文件的输出的目标目录和文件的后缀名,执行copy的作(因为输出格式为按文件的后缀名分类,即输出如:pdf目录下面有pdf文件,txt目录下面有txt文件)
copy(file, new File(outputPath, suffix));
} else {
//4.3.如果该文件没有后缀名,即不包含“.”点符号,则直接在文件的输出的目标目录下建立“nosuffix”目录,执行copy的作,将没有后缀名的文件到nosuffix目录下
copy(file, new File(outputPath, "nosuffix"));
单个文件作
@param sourceFile
@param targetDir
private static void copy(File sourceFile , File targetDir){
//1.如果目标目录不存在,则新建,此处的目标目录为按文件后缀名命名的目录,如pdf目录,txt目录以及nosuffix目录
if(!targetDir.exists()){
targetDir.mkdir();//新建目录
}try {
//2.将源文件读取到输入流中
FileInputStream fis = new FileInputStream(sourceFile);
//3.按照目标目录和文件名构建文件输出流
FileOutputStream fos = new FileOutputStream(new File(targetDir , sourceFile.getName())) ;
//4.定义缓冲区,大小为102400byte
byte[] buf = new byte[102400];
//5.定义输入流读取到的文件大小,下面会用到
int ailable = 0;
//6.fis.ailable():使用输入流fis读取当前文件的大小,将读取到的大小赋值给ailable
while((ailable = fis.ailable()) > buf.length){
//7.如果当前读取到的大小ailable大于定义的缓冲区的大小102400,则用fis.read(buf)方法读取文件。
//此时,缓冲区buf会被完全读满,即源文件的前102400 byte的数据都会被读取到buf中,
//fis下一次读取就会从102401 byte开始读,即文件读取的开始位置移动到了ailable+1的位置上
fis.read(buf);
//8.使用文件输入流fos将缓冲区buf中的内容写入到目标文件中
fos.write(buf);
}//9.上面的循环始终会结束,因为文件的大小不可能无限的大,当上面循环的判断使用fis.ailable()读取到当前剩余的文件大小小于102400 byte时,
//使用fis.read(buf, 0, ailable)将剩余的文件数据读取的buf对应的位置,
//例如,剩下的文件数据大小为100 byte,即ailable的值为100,那么使用执行方法fis.read(buf, 0, ailable),后,
//buf数组中的buf[0]到buf[99]会有数据,buf[100]之后的就都是空了。
fis.read(buf, 0, ailable);
//10.调用输出流fos.write(buf, 0, ailable)表示将buf中的前ailable大小的数据输出,
//例如上面举得例子,就只会将buf中的前100位数据输出。
fis.close();
fos.close();
} catch (Exception e) {
弹出文件选择对话框,选择要被的源目录
@return
private static File getSourcePath(){
//1.弹出文件选择对话框
JFileChooser chooser = new JFileChooser();
//2.设置只能选择目录
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//3.返回选择的目录,如果没有选择则返回null
if(chooser.showOpenDialog(null) == JFileChoo/ser.APPROVE_OPTION){
return chooser.getSelectedFile();
}return null;
}/
弹出对话框,选择文件后存放的目标目录
@return
//1.弹出文件选择对话框
JFileChooser chooser = new JFileChooser();
//2.设置只能选择目录
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//3.返回选择的目录,如果没有选择则返回null
return chooser.getSelectedFile();
}return null;
ja文件流输入FileInputStream类中如何判断文件打开是否成功
文件字节输入流可以从指定路径的文件中读取字节数据。文件字节输入流类继承InputStream类,并实现了读取输入流的各种方法。也System.out.println("copying " + sourceFile);就是判断文件是否存在?
例如:
File file=new File(("c:janan.txt");
if(file.exist())FileInputStream inFile=new FileInputStream(file);
也就是判断文件是否存在?
例如:
File file=new File(("c:janan.txt");
if(file.exist())FileInputStream inFile=new FileInputStream(file);
你应该问的是文件是否存在吧??
File file=new File("xxx.xls");
//判断文件是否存在
if(file.exists){
FileInputStream fis=new FileInputStream(file);
}
InputStreamReader isr = new InputStreamReader(new FileInputStream("."));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}
Ja编程!键盘输入一段英文语句,将这段话写入文件file.txt中,然后统计并输出这段话中英文单词的数目。
import ja.io.File;
import ja.io.FileInputStream;
import ja.i}}o.FileWriter;
import ja.util.Scanner;
public class Test2 {
Scanner san = new Scanner(System.i{bw.close();n);
System.out.println("Input the text:");
String strText = san.nextLine();
String[] strArr = strText.split(" ");
File file = new File("file.txt");
FileWriter input = new FileWriter(file);
input.write(strText);
input.close();
System.out.println("单词的数目" + strArr.length);
我用fileWriter和bufferedwriter写入文件,为什么写进去就是乱码呢?
if(chooser.showSeDialog(null) == JFileChooser.APPROVE_OPTION){使用字节流OutputStream可以直接输出,现在要是用到字符流处理的话,也当年困扰到我,现在将分析码写下来共参考:
//FileReader fr = new FileReader("d:/d.txt");
//BufferedReader br = new BufferedReader(fr);
以上注释是常规写法,处理英文好使,处理中文就改为以下写法
InputStreamReader brs = new InputStreamRead可以先实例一个File对象!方法exist()检查File是否存在!!er(fr,"GBK");
BufferedReader br = new BufferedReader(brs); 把filewriter的写法写成FileinputStream形式,在GBK即可。
ja OutputStream使用方法
public static void main(String[] args) throws Exception{1 字节转为字符,String有个构造函数,参数是字节数组,用这个就行了。
2 getBytes是string的方法,加了这个应该是为了统一,如果你读的是GBK的,写的是UTF8的,可能会有乱码
你怎么拆的就怎么拼装回去呗new String(getyourstr.getBytes("GB2312"))
getByte("GB2312")是把你的字符串按GB2312格式编码后转换成字节数组
可是一般是这么用的new String(getyourstr.getBytes("iso-8859-1"),"GB2312"),可以避免乱码
你这么写法不知道是要干什么
OutputStream是流的形式,具体可以表现为FileOutputStream的形式进行整行的写入。
可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:
Output/StreamWriter pw = null;//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系 836084111@qq.com 删除。