package io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class SystemDemo {
public static void main(String[] args) throws FileNotFoundException, IOException {
SystemDemo sd = new SystemDemo();
//sd.getNameAndPassword();
//sd.getFileInputStreamDemo();
//sd.getFileOutputStreamDemo();
//sd.CopyFile();
//sd.getProperties();
InputStreamReader isr = null;
BufferedReader br = null;
FileWriter fWriter = null;
BufferedWriter bWriter = null;
try{
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
//定义一个输出的日志文件
File file = new File("d:/Jery/homework/1.txt");
//创建文件的文件夹,不存在就为null 如果中间文件夹不存在的话就自动创建。
File parentFile = file.getParentFile();
if(parentFile!=null && !parentFile.exists()){
parentFile.mkdirs();
}
//true 表示这个文件不会被重新生成,会覆盖。
fWriter = new FileWriter(file,true);
bWriter = new BufferedWriter(fWriter);
String str = "";
//如果输入 quit 直接退出。readLine() 一行一行的读取。
while(!"quit".equals(str = br.readLine())){//quit
bWriter.write(str);
bWriter.newLine();
};
} catch (Exception e) {
// TODO: handle exception
}finally {
try {
if(bWriter!=null)bWriter.close();
if(fWriter!=null)fWriter.close();
if(br!=null)br.close();
if(isr!=null)isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void getNameAndPassword(){
//确定是读 的方式 System.in
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
try {
System.out.println("请输入您的姓名:");
//一行一行的读
String name = br.readLine();
System.out.println("请输入您的密码:");
Integer password = Integer.parseInt(br.readLine());
System.out.println("您输入的用户名为:"+name+"密码为:"+password);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getFileInputStreamDemo(){
/*
* 输入inputStream 输出流outputStream 以1byte的方式去读或写入 byte[]
* 字节流 stream :用于进行文件的复制粘贴和处理二进制数据----》图片/视频、音频
*
* 字符流 用于操作文本文件的内容,例如添加删除修改里面的内容。---->(txt/java/jsp/xml)
* 以2个byte的单位进行读取和写入 ----》char[]
*
* 缓冲流 叫你读写更高效
*
* */
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
//创建一个文件对象
//File file = new File("src/1.txt");
File file = new File("src/wicket-xhtml1.4-strict.dtd");
try {
//创建字节流文件输入流对象
inputStream = new FileInputStream(file);
//用read()方法去读
//int num = inputStream.read();
int len = 0 ;
//当读到最后一个的时候是-1
/*while ((len=inputStream.read())!=-1) {
System.out.print((char)len+"");
}*/
byte[] bt = new byte[1024];
while ((len=inputStream.read(bt))!=-1) {
System.out.print(len+"");
//转换的bt数组,开始的数组,结束的数组。这个样子就不会在最后一次也打印 1024个字节了。
System.out.println(new String(bt,0,len));
}
/*//用 read()的 byte[]方式去读
byte[] bs = new byte[1024]; //1025个字节
while((len=inputStream.read(bs))!=-1){
System.out.print(len+" ");
System.out.println(new String(bs));
}
*/
/* byte[] bt = new byte[1024]; //1024 中
long total = file.length();//1028
float num = 0f;
while((len=inputStream.read(bt))!=-1){
System.out.println("======"+len);
num += len;
System.out.println(new String(bt));
//数字格式化
System.out.println(new DecimalFormat("#.##").format(num/total));
System.out.println(num/total);
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
public void getFileOutputStreamDemo(){
//字节输出流
File file = new File("src/test.txt");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
String contents = "qweqwe李薇薇";
byte[] bt = contents.getBytes();
//OutputStreamWriter out = new OutputStreamWriter(outputStream, "utf-8");
outputStream.write(bt);
//out.write(contents);
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("创建文件test.dtd成功!!!");
try {
if(outputStream!=null)outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void CopyFile(){
//获取要复制的文件路径
File file = new File("C:/Users/Administrator/Documents/Tencent Files/1273091433/FileRecv/大数据云计算架构师.rar");
//存放位置
File file2 = new File("d:/111.rar");
if(!file2.exists()){
try {
//判断文件是否存在,如果不存在就新建
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//输入流
FileInputStream in = null;
//输出流
FileOutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(file2);
//byte[]可以存储多少 字节一般都是 读取 1kb 多少没有限制我试了一下 60MB依然可以读取出来。
byte[] b = new byte[1024];
System.out.println("开始复制文件。。。");
//记录时间
Long starTime = System.currentTimeMillis();
int len = 0;
//开始复制
while ((len=in.read(b))!=-1) {
System.out.println(len+" ");
out.write(b, 0, len);
}
//复制结束时间
Long lastTime = System.currentTimeMillis();
System.out.println("复制完成!");
System.out.println("一共用时:"+(lastTime-starTime));
} catch (Exception e) {
e.printStackTrace();
}finally{
//开流要记得关流。
try {
if(out!=null)out.close();
if(in!=null)in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void getProperties() throws FileNotFoundException, IOException{
//创建一个配置文件对象
Properties properties = new Properties();
properties.load(new FileInputStream("d:/a.properties"));
System.out.println(properties);
System.out.println(properties.getProperty("age"));
//判断键 是否存在
System.out.println(properties.containsKey("liwei"));
// 获取JVM的系统属性
Properties ps = System.getProperties();
ps.list(System.out);
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class SystemDemo {
public static void main(String[] args) throws FileNotFoundException, IOException {
SystemDemo sd = new SystemDemo();
//sd.getNameAndPassword();
//sd.getFileInputStreamDemo();
//sd.getFileOutputStreamDemo();
//sd.CopyFile();
//sd.getProperties();
InputStreamReader isr = null;
BufferedReader br = null;
FileWriter fWriter = null;
BufferedWriter bWriter = null;
try{
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
//定义一个输出的日志文件
File file = new File("d:/Jery/homework/1.txt");
//创建文件的文件夹,不存在就为null 如果中间文件夹不存在的话就自动创建。
File parentFile = file.getParentFile();
if(parentFile!=null && !parentFile.exists()){
parentFile.mkdirs();
}
//true 表示这个文件不会被重新生成,会覆盖。
fWriter = new FileWriter(file,true);
bWriter = new BufferedWriter(fWriter);
String str = "";
//如果输入 quit 直接退出。readLine() 一行一行的读取。
while(!"quit".equals(str = br.readLine())){//quit
bWriter.write(str);
bWriter.newLine();
};
} catch (Exception e) {
// TODO: handle exception
}finally {
try {
if(bWriter!=null)bWriter.close();
if(fWriter!=null)fWriter.close();
if(br!=null)br.close();
if(isr!=null)isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void getNameAndPassword(){
//确定是读 的方式 System.in
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
try {
System.out.println("请输入您的姓名:");
//一行一行的读
String name = br.readLine();
System.out.println("请输入您的密码:");
Integer password = Integer.parseInt(br.readLine());
System.out.println("您输入的用户名为:"+name+"密码为:"+password);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getFileInputStreamDemo(){
/*
* 输入inputStream 输出流outputStream 以1byte的方式去读或写入 byte[]
* 字节流 stream :用于进行文件的复制粘贴和处理二进制数据----》图片/视频、音频
*
* 字符流 用于操作文本文件的内容,例如添加删除修改里面的内容。---->(txt/java/jsp/xml)
* 以2个byte的单位进行读取和写入 ----》char[]
*
* 缓冲流 叫你读写更高效
*
* */
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
//创建一个文件对象
//File file = new File("src/1.txt");
File file = new File("src/wicket-xhtml1.4-strict.dtd");
try {
//创建字节流文件输入流对象
inputStream = new FileInputStream(file);
//用read()方法去读
//int num = inputStream.read();
int len = 0 ;
//当读到最后一个的时候是-1
/*while ((len=inputStream.read())!=-1) {
System.out.print((char)len+"");
}*/
byte[] bt = new byte[1024];
while ((len=inputStream.read(bt))!=-1) {
System.out.print(len+"");
//转换的bt数组,开始的数组,结束的数组。这个样子就不会在最后一次也打印 1024个字节了。
System.out.println(new String(bt,0,len));
}
/*//用 read()的 byte[]方式去读
byte[] bs = new byte[1024]; //1025个字节
while((len=inputStream.read(bs))!=-1){
System.out.print(len+" ");
System.out.println(new String(bs));
}
*/
/* byte[] bt = new byte[1024]; //1024 中
long total = file.length();//1028
float num = 0f;
while((len=inputStream.read(bt))!=-1){
System.out.println("======"+len);
num += len;
System.out.println(new String(bt));
//数字格式化
System.out.println(new DecimalFormat("#.##").format(num/total));
System.out.println(num/total);
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
public void getFileOutputStreamDemo(){
//字节输出流
File file = new File("src/test.txt");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
String contents = "qweqwe李薇薇";
byte[] bt = contents.getBytes();
//OutputStreamWriter out = new OutputStreamWriter(outputStream, "utf-8");
outputStream.write(bt);
//out.write(contents);
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("创建文件test.dtd成功!!!");
try {
if(outputStream!=null)outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void CopyFile(){
//获取要复制的文件路径
File file = new File("C:/Users/Administrator/Documents/Tencent Files/1273091433/FileRecv/大数据云计算架构师.rar");
//存放位置
File file2 = new File("d:/111.rar");
if(!file2.exists()){
try {
//判断文件是否存在,如果不存在就新建
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//输入流
FileInputStream in = null;
//输出流
FileOutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(file2);
//byte[]可以存储多少 字节一般都是 读取 1kb 多少没有限制我试了一下 60MB依然可以读取出来。
byte[] b = new byte[1024];
System.out.println("开始复制文件。。。");
//记录时间
Long starTime = System.currentTimeMillis();
int len = 0;
//开始复制
while ((len=in.read(b))!=-1) {
System.out.println(len+" ");
out.write(b, 0, len);
}
//复制结束时间
Long lastTime = System.currentTimeMillis();
System.out.println("复制完成!");
System.out.println("一共用时:"+(lastTime-starTime));
} catch (Exception e) {
e.printStackTrace();
}finally{
//开流要记得关流。
try {
if(out!=null)out.close();
if(in!=null)in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void getProperties() throws FileNotFoundException, IOException{
//创建一个配置文件对象
Properties properties = new Properties();
properties.load(new FileInputStream("d:/a.properties"));
System.out.println(properties);
System.out.println(properties.getProperty("age"));
//判断键 是否存在
System.out.println(properties.containsKey("liwei"));
// 获取JVM的系统属性
Properties ps = System.getProperties();
ps.list(System.out);
}
}