输入:将文件以数据流的形式读取到java程序中
输出:通过java程序将数据流写入文件中
文章目录
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Test {
public static void main(String[] args) {
Reader reader = null;
try {
reader = new FileReader("D:\\java\\test.txt");
char[] chars = new char[100];
reader.read(chars);
for (char aChar : chars) {
System.out.println(aChar);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.*;
public class Test2 {
public static void main(String[] args) {
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
//节点流
inputStream = new FileInputStream("D:\\java\\test.txt");
//缓冲流
bufferedInputStream = new BufferedInputStream(inputStream);
int temp = 0;
while((temp = bufferedInputStream.read())!=-1){
System.out.println(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
try {
inputStream.close();
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用缓冲流之后,一定要注意:在 close 之前必须先 flush,确保缓冲区中的数据已经全部传到了文件中,再来关闭流,否则会抛出 Stream closed 异常。
指文件夹,文件是指图片、txt、word等等
| 方法 | 描述 |
|---|---|
| public File(String pathname) | 根据路径创建文件对象 |
| public String getName() | 获取文件名 |
| public String getParent() | 获取文件所在目录 |
| public File getParentFile() | 获取文件所在目录对应的对象 |
| public String getPath() | 获取文件路径 |
| public boolean exists() | 判断文件是否存在 |
| public boolean isDirectory() | 判断对象是否为目录 |
| public boolean isFile() | 判断对象是否为文件 |
| public long length() | 获取文件大小,以byte为单位 |
| public boolean createNewFile() | 根据当前File创建新的文件 |
| public boolean delete() | 删除文件 |
| public boolean mkdir() | 根据当前对象创建新目录 |
| public boolean renameTo(File file) | 为已存在的对象重命名 |
import java.io.File;
import java.io.IOException;
public class Test3 {
public static void main(String[] args) {
File file = new File("D:\\java\\123.png");
if(file.exists()){
System.out.println(file.getName());
System.out.println(file.length());
System.out.println(file.getParent());
System.out.println(file.getPath());
File parentFile = file.getParentFile();
System.out.println(parentFile.getName());
System.out.println(parentFile.isDirectory());
System.out.println(file.isDirectory());
System.out.println(parentFile.isFile());
System.out.println(file.isFile());
}
try {
File file2 = new File("D:\\java\\123.png");
System.out.println(file2.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
JAVA 的IO流类库基本能满足需求,通过继承话可以创建新的输入输出对象。
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht