java IO的学习

File文件类的使用:

我在桌面放一个文件:10.jpg 路径:C:\\Users\\Administrator\\Desktop\\10.jpg

                  一个文件夹:test 里面放一个文件mysql.txt

public class TestIO {	
  public static void main(String[] args) {
	  File file = new File("C:\\Users\\Administrator\\Desktop\\10.jpg");
	  File file1 = new File("C:\\Users\\Administrator\\Desktop\\test");
	  File file2 = new File(file1,"mysql.txt");
	  System.out.println(file.exists());//文件是否存在
	  System.out.println(file1.exists());
	  System.out.println(file2.exists());
	  System.out.println(file.getAbsolutePath());//获取完全的路径
	  System.out.println(file.getParentFile());//获取上一级路径
	  System.out.println(file.canWrite());//文件是否可写
  }
}

输出结果:

true
true
true
C:\Users\Administrator\Desktop\10.jpg
C:\Users\Administrator\Desktop
true

其实File的构造器还有其他的,我比较常用的是第一个,指定绝对路径的。File类能操作文件本身,但是

不能操作文件的内容啥的,操作流时,要使用到文件,所以先介绍了下文件。

1、字节流:

InputStream:输入字节流,一个字节为8位,用于视频、图片等文件的操作。

典型的实现类:FileInputStream

在桌面上创建一个文件,my.txt 里面写入:hello,my name is cjh....

public class TestIO2 {	
  public static void main(String[] args) {
	  //hello,my name is cjh....
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my.txt");
	  InputStream in   = null;
	  try {
		  in           = new FileInputStream(fileIn);
		  byte[] b     = new byte[200];
		  int len      = 0;
		  while((len = in.read(b))!=-1) {
			   String result = "";
			   result        = new String(b);		   
			   System.out.print(result);
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			in.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}

可以看到控制台打印:hello,my name is cjh....

这说明已经把文件中的内容读进来了。

OutputStream:输出字节流

典型的实现类:FileOutputStream.

public class TestIO3 {	
  public static void main(String[] args) {
	  //hello,my name is cjh....
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\my1.txt");
	  OutputStream out   = null;
	  try {
		  out           = new FileOutputStream(fileOut);
		  out.write("hello,i am cjh...".getBytes());
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			out.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}

执行代码之后,会在桌面生成一个my1.txt文件,打开可以看到: hello,i am cjh...


这里做一个综合的练习,将桌面的文件10.jpg写到test目录下:

public class TestIO {
  public static void main(String[] args) {
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\10.jpg");
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\test\\"+fileIn.getName());
	  InputStream in   = null;
	  OutputStream out = null;
	  try {
		  //读入输入流
		  in = new FileInputStream(fileIn);
		  byte[] b  = new byte[1024];
		  int lent  = 0;
		  //指定输出流
		  out       = new FileOutputStream(fileOut);
		  while((lent=in.read(b))!=-1) {
			  out.write(b, 0, lent);
		  }
	  } catch (Exception e) {
		  e.printStackTrace();
	  }finally {
		  //记住要关闭流
		  try {
			in.close();
			 out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	 
	  }
  }
}
要指定输出文件的具体路径和名称,那么会将输入文件的内容,写入到输出文件,则相当于实现了文件的复制功能。


2、字符流

Reader:输入字符流;用于文本文件的传输,因为一些字符可能是两个字节的,比如中文。

典型的实现:FileReader

在桌面添加一个my2.txt文件,写入:本来讨厌下雨的天空,突然听见有人说爱我。

public class TestIO4 {	
  public static void main(String[] args) {	  
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my2.txt");
	  Reader reader   = null;
	  try {
		  reader           = new FileReader(fileIn);
		  char[] c  = new char[1024];
		  int len   = 0;
		  while((len=reader.read(c))!=-1) {
			  String result = new String(c, 0, len);
			  System.out.println(result);
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			reader.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }		  
  }
}
执行程序,控制台输出:本来讨厌下雨的天空,突然听见有人说爱我。

这里我再用上述的字节输入流来读中文和英文都有的文本,看看出现什么问题:

在桌面的my.txt文件中,将内容改为:a最美的不是下雨天,而是与你躲过雨的屋檐。

public class TestIO2 {	
  public static void main(String[] args) {
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my.txt");
	  InputStream in   = null;
	  try {
		  in           = new FileInputStream(fileIn);
		  byte[] b     = new byte[4];
		  int len      = 0;
		  while((len = in.read(b))!=-1) {
			   String result = "";
			   result        = new String(b);		   
			   System.out.print(result);
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }	  
  }
}

输出结果:a最?赖牟皇窍掠晏欤怯肽愣愎甑奈蓍堋i堋

当然,如果也能读到时正确的字符,如果字节数刚好匹配的话。试着改成:aa最美的不是下雨天,而是与你躲过雨的屋檐

再执行程序,就会发现是正确的输出。

当然字符流就不会出现这个问题,它才不管你几个字节,就是按照一个字符来处理,读的时候就是读的字符。


Writer:字符输出流。

典型实现:FileWriter

public class TestIO5 {	
  public static void main(String[] args) {	  
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\my3.txt");
	  Writer writer   = null;
	  try {
		  writer           = new FileWriter(fileOut);
		  writer.write("听妈妈的话,别让她受伤。");
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			  writer.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}
执行程序,你会在桌面上发现创建了一个my3.txt的文件,打开,里面的内容为:听妈妈的话,别让她受伤。

做一个练习,将刚才桌面上的my2.txt文件写入到test目录下。

public class TestIO6 {
	public static void main(String[] args) {	
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my2.txt");
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\test\\my2.txt");
	  Reader reader   = null;
	  Writer writer   = null;
	  try {
		  reader           = new FileReader(fileIn);
		  writer           = new FileWriter(fileOut);
		  char[] c  = new char[2];
		  int len   = 0;
		  while((len=reader.read(c))!=-1) {
			 writer.write(new String(c));
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			reader.close();
			writer.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }		  
  }
}
执行程序,在test目录下会有my2.txt文件生成,同时内容和桌面的文件是一样的。


3、缓冲流

public class TestIO7 {
	public static void main(String[] args) {	
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my2.txt");
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\test\\my3.txt");	
	  BufferedReader bufReader = null;
	  BufferedWriter bufWriter = null;
	  try {		 
		  bufReader        = new BufferedReader(new FileReader(fileIn));
		  bufWriter        = new BufferedWriter(new FileWriter(fileOut));
		  char[] c  = new char[2];
		  int len   = 0;
		  while((len=bufReader.read(c))!=-1) {
			  bufWriter.write(new String(c));
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
		   if( bufWriter != null && bufReader != null ) {	
				bufWriter.close();
				bufReader.close();
		   }			
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}

执行程序,在test目录下会生成my3.txt文件,内容和my2.txt一样。

打开链接下载源码: https://pan.quark.cn/s/05da658a2377 在信息技术领域中,输入法作为操作系统的一个核心构成部分,赋予了用户利用键盘输入多语种文字的能力。"ime-日语输入法安装必须文件"这一资源是一套为日语输入法部署而设计、包含全部必要元素的集成包,对于那些需要在个人计算机上执行日语文字输入的操作者而言具有不可替代的作用。接下来将深入剖析其中所包含的核心概念。 IME(Input Method Editor,输入法编辑器)是操作系统内的一种软件支持服务,其功能在于为非拉丁字符环境提供文字输入方案,例如中文、日文、韩文等文字系统。在日本地区,IME通常被用来将罗马字(罗马拼音)形式的输入转换为平假名、片假名乃至汉字。此压缩文件内含的日语IME文件夹即为执行这一转换功能的关键要素。 kbdjpn.dll被视为一个关键的系统性文件,其意指“Japanese Keyboard Layout”(日语键盘布局)。该动态链接库文件负责设定日语键盘的排列方式及快捷操作组合,使用户能够借助常规的QWERTY键盘输入日语文字。倘若缺少这一文件,即便已经安装了日语输入法,依然无法正常显示及输入日语字符。 另外,imjp81k.dll同样是一个重要的系统性构成,它属于日语IME的范畴,全称为“Input Method Japanese for Windows 8.1 and later, Katakana mode”(适用于Windows 8.1及更新版本的日语输入法,片假名模式)。该文件支持日语的片假名输入,是处理日语输入的核心组成部分。在安装或升级日语输入法的过程中,保证imjp81k.dll的准确性与完整性显得尤为关键。 压缩包所含的"Window...
内容概要:本文研究了基于DPWMA调制与正负序分离的ANPC三电平并网逆变器前馈控制策略,旨在解决传统三电平逆变器在谐波抑制、电网不平衡适应性及动态响应方面的技术瓶颈。通过构建融合双极性倍频脉宽调制(DPWMA)、正负序分离锁相控制与电网电压前馈的一体化控制体系,全面优化逆变器的输出波形质量、相位同步精度与抗扰能力。文章深入分析了ANPC三电平拓扑的结构优势,如开关损耗均衡、中点电位可控性强和电压利用率高等特点,并设计了包含信号采集、核心控制与调制驱动三层架构的完整控制系统。通过Simulink仿真平台对稳态运行、电网不平衡及动态扰动等多种工况进行验证,结果表明该策略显著降低了总谐波畸变率,提升了锁相精度与系统动态稳定性,有效增强了逆变器在复杂电网环境下的适应能力和运行可靠性。; 适合人群:具备电力电子、自动控制及新能源并网相关基础知识,从事新能源发电、微电网、电力系统仿真等领域的科研人员与工程技术人员,特别适合研究生及以上层次的研究者。; 使用场景及目标:①用于提升大功率并网逆变器在电网电压不平衡、谐波干扰和动态扰动等复杂工况下的运行性能;②为高电能质量要求的应用场景提供先进控制解决方案;③支持科研仿真、论文复现与实际工程项目中的高性能并网控制系统设计与优化。; 阅读建议:建议结合提供的Simulink仿真模型进行实践操作,重点理解DPWMA调制机制、正负序分离锁相算法与电网电压前馈控制之间的协同作用,按照文档结构系统学习,并与传统控制策略进行对比分析,以深入掌握改进策略的技术优势与实现细节。
代码下载链接: https://pan.quark.cn/s/d9794888cbc0 ### G代码经典解释程序知识点详解 #### 一、引言 随着数控技术的持续进步,尤其是开放式数控系统的广泛应用,软件层面的设计在数控领域占据了核心地位。G代码作为数控机床编程的基础语言,在自动化生产流程中发挥着不可或缺的作用。本文的核心内容是关于一个基于Linux平台、采用C语言开发的G代码解释程序的设计思路及其具体实现。 #### 二、G代码解释器概述 **1. 设计背景** - 当前数控技术发展的主要方向是开放式数控系统,这类系统具备出色的可扩展能力、良好的移植性、高度的互换性以及优异的互操作性等优势。 - 计算机硬件技术的快速发展使得在PC平台上构建数控系统成为可能,进而推动了全软件式数控系统的普及。 **2. G代码解释器的重要性** - G代码解释器在全软件式数控系统中是至关重要的组成部分,其主要职责是将G代码转化为数控系统能够识别的数据格式。 - 为了提升数控系统的开放程度,G代码解释器的设计必须兼顾开放性和灵活性。 #### 三、G代码解释器设计与实现 **1. 总体结构设计** - G代码解释器主要由两个核心部分构成:G代码关键字函数表(GKFT)和G代码分组(GG)。 - GKFT用于解析G代码中的关键字,它是解释器的核心骨架;而GG则是语法检查的基础框架。 **2. G代码关键字函数表(GKFT)** - GKFT是一种专门用于存储G代码关键字及其关联处理函数的数据结构。 - 解释器通过查询GKFT,能够根据特定的G代码关键字调用相应的处理函数,从而完成对G代码的有效解析。 - 此种设计方法不仅简化了解释器的构建过程,同时也增强了其可扩展性,因为新增功能...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值