FileInputStream:字节输入流读取
注意:字节输入流,到-1就是文件内容的末尾
//FileInputStream字节输入流读取
public class FileInputStreamDemo5 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\ps.txt");
/*fis.read()读数据
by=fis.read()把读取到的数据赋值给变量by
!=1判断by的值是不是等于负1
* */
int by;
while((by=fis.read())!=-1){
System.out.println((char)by);
}
fis.close();
}
}
6、文件复制
注意:其实就是文件读取时,同时再写入文件
//文件复制:其实就是文件读取时,同时再写入文件
public class FileInputStreamDemo6 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\ps.txt");
FileOutputStream fos = new FileOutputStream("f:\\test\\ios.txt");
int by;
while((by=fis.read())!=-1){
fos.write(by);
}
fis.close();
fos.close();
}
}
7、读取字节数组
注意:数组的字节长度通常是1024及其倍数
//读取字节数组
public class FileInputStreamDemo7 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\ios.txt");
byte[] bytes = new byte[1024];//长度一般设置为1024及其倍数
int len;
while((len=fis.read(bytes))!=-1){
System.out.print(new String(bytes,0,len));
}
fis.close();
}
}
8、复制图片案例
注意:读取字节的同时,同时存入字节
//读取和写入图片
public class FileInputStreamDemo8 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\img\\12.png");
FileOutputStream fos = new FileOutputStream("f:\\test\\image\\12.png");
byte[] bytes = new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
}
9、字节缓冲流
//字节缓冲流
public class FileInputStreamDemo9 {
public static void main(String[] args) throws IOException {
//创建字节缓冲区输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:\\test\\ps.txt"));
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
bos.close();
//创建字节缓冲区输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("f:\\test\\ps.txt"));
//第一方式,读取单个字节
int by;
while((by=bis.read())!=-1){
System.out.print((char)by);
}
//第二种方式读取字节数组
byte[] bys = new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
bis.close();
}
}
10、案例:复制视频
//四种字节流方式读写视频
public class FileInputStreamDemo10 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\vadio1\\2.mp4");
FileOutputStream fos = new FileOutputStream("f:\\test\\vadio2\\2.mp4")*/;
//第一种:单个字节流
int by;
while((by=fis.read())!=-1){
fos.write(by);
}
//第二种:单个字节数组
byte[] bytes = new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("f:\\test\\vadio1\\3.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:\\test\\vadio2\\3.mp4"));
//第三种:字节缓冲区单个字节
int by;
while((by=bis.read())!=-1){
bos.write(by);
}
//第四种:字节缓冲区数组
byte[] bytes = new byte[1024];
int len;
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
11、字符串编码与解码
编码格式与解码格式需要对应
UTF-8:三个字符表示一个汉字
GBK: 两个字符表示一个汉字
//字符串编码与解码
public class StringDemo1 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "中国";
/* 第一种方式编码
byte[] bys1 = s.getBytes();//系统默认编码格式UTF-8
System.out.println(Arrays.toString(bys1));
//第二种方式编码
byte[] bys2 = s.getBytes("UTF-8");//指定UTF-8格式:一个汉字3个字符
System.out.println(Arrays.toString(bys2));
//第三种方式编码
byte[] bys3 = s.getBytes("GBK");//指定GBK格式:一个汉字2个字符
System.out.println(Arrays.toString(bys3));*/
//解码第一种方式:系统默认
byte[] bys1 = s.getBytes();
String s1 = new String(bys1);
System.out.println(s1);
//解码第二种方式:指定utf-8
byte[] bys2 = s.getBytes();//系统默认编码就是utf-8
String s2 = new String(bys2, Charset.forName("utf-8"));
System.out.println(s2);
//解码第三种方式:指定GBK
byte[] bys3 = s.getBytes("gbk");//编码GBK
String s3 = new String(bys3, Charset.forName("gbk"));//解码GBK
System.out.println(s3);
}
}
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]