我有一个初始化文件(initialize.java),它使用fileInputStream从fileInput.txt中提取数据,但它们都位于不同的目录中.
Run Code Online (Sandbox Code Playgroud)Project/library/initialize.java Project/resources/text/fileInput.txt
我在initialize.java中的代码是:
FileInputStream fstream = new FileInputStream("/resources/text/fileInput.txt");
Run Code Online (Sandbox Code Playgroud)
但是文件无法读取.我也试过了
FileInputStream fstream = new FileInputStream("./resources/text/fileInput.txt");
Run Code Online (Sandbox Code Playgroud)
但那也行不通.
如何访问txt文件以及使用"./resources"和"/ resources"之间的区别是什么?
感谢您阅读本文.
是否可以使用java读取日志文件(abc.log)?
我想从我的日志文件中获取特定的字符串.
假设这是我的日志文件的内容.我只想要时间戳(例如:05:08:37)并将其打印到控制台.
2012-12-16 05:08:37,905 [Thread-1] INFO com.submit.SubmitService - Wait time 500
2012-12-16 05:08:38,444 [Thread-1] INFO com.submit.SubmitService - NO OF RECORDS TILL NOW 3755 TOTAL TIME -- << 539
2012-12-16 05:08:38,668 [Thread-1] INFO com.submit.SubmitService - Active Connection:: -69076
2012-12-16 05:08:38,670 [Thread-1] INFO com.submit.SubmitService - Active Connection:: -65764
Run Code Online (Sandbox Code Playgroud) 我正在为我的android应用程序开发一项新功能,以启用数据备份和还原。我正在使用XML文件备份数据。这是一段代码,用于设置输出文件的编码:
XmlSerializer serializer = Xml.newSerializer();
FileWriter fileWriter = new FileWriter(file, false);
serializer.setOutput(fileWriter);
serializer.startDocument("UTF-8", true);
[... Write data to the file....]
Run Code Online (Sandbox Code Playgroud)
这就是我尝试从XML文件导入数据的方式。首先,我检查编码是否正确:
XmlPullParser parser = Xml.newPullParser();
FileReader reader = new FileReader(file);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(reader);
if(!"UTF-8".equals(parser.getInputEncoding())) {
throw new IOException("Incorrect file encoding");
}
[... Read data from the file....]
Run Code Online (Sandbox Code Playgroud)
在这里,我遇到了一个问题。此代码在Android 2.3.3(设备和仿真器)上均能正常工作,该编码已正确检测为“ UTF-8”。但是在API11 +版本(蜂窝,ICS,JB)上会引发异常。当我在调试模式下运行此命令时,我可以看到parser.getInputEncoding()返回null。我检查了在2.3.3和更高版本上生成的实际XML文件,它们具有完全相同的标题:<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>。为什么getInputEncoding()在API11 +上返回null?
其他发现:
我发现有一种方法可以使用FileInputStream而不是FileReader像这样来正确检测API11 +设备上的文件编码:
XmlPullParser parser = Xml.newPullParser();
FileInputStream stream = new FileInputStream(file);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(stream, …Run Code Online (Sandbox Code Playgroud) 对于大型文件或管道流,缓冲支持的解决方案(BufferedInputStream/ByteArrayInputStream)显然不是可行的方法.如果有人能告诉我处理这种情况的推荐方法,我将不胜感激.
我可以想到这一点 - 但可能不是那里最好或最有效的方法:
public class Streams {
public static void main(String[] args) throws IOException {
DataInputStream reader=null;
try{
try {
reader=new DataInputStream(new FileInputStream("/path/file"));
} catch (FileNotFoundException e1) {
throw e1;
}
while(true) {
try {
byte a=reader.readByte();
}
catch(EOFException e) {
//consume
}
catch (IOException e) {
//throw
throw e;
}
//do something
}
}
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个bean需要从属性文件中获取一些参数但我找不到它(java.lang.NullPointerException)才能打开它.我的bean在extra.beans包中,而属性文件在extra.dao包中.我想做
file = new FileInputStream("database.properties");
prop.load(file);
Run Code Online (Sandbox Code Playgroud)
我已尝试任何可能的组合路径,但我找不到它.我正在使用Netbeans 7.4.我该怎么打开它?
对于作业,我们需要能够保存自定义文件类型,然后能够再次读取它。我想我可以使用 FileInputStream 类,这样我就可以轻松地使用 myInStream.read() 从文件中获取我需要的所有信息。但是,我在将文件放入 FileInputStream 时遇到了问题。我能够获取 URI,但似乎我用来获取 URI 的方法使 URI 类型与 File 类不兼容。在我开始阅读之前,我的 onActivityResult() 中有以下代码:
Uri pickedFile = data.getData();
File myFile = new File(pickedFile);
FileInputStream inStream = new FileInputStream(myFile);
Run Code Online (Sandbox Code Playgroud)
在第二行它说“无法解析构造函数‘文件(android.net.Uri)’”,所以我假设我必须将 URI 转换为不同的格式,但我不知道如何去做。任何帮助将非常感激!
编辑:为了将来参考,固定代码如下所示:Uri pickFile = data.getData(); InputStream inStream = getContentResolver().openInputStream(pickedFile);
我有一个.properties文件,其格式如下:
toto=titi
fofo=fifi
coco=cici
mama=momo
dada=didi
Run Code Online (Sandbox Code Playgroud)
当我解析这个文件时,我有一个奇怪的显示.这是我正在使用的代码:
Properties prop = new Properties();
String fileLocation = "C:/myProperties.properties";
prop.load(new FileInputStream(fileLocation));
Iterator<Object> it = prop.keySet().iterator();
int line = 0;
while (it.hasNext())
{
String propertyName = (String) it.next();
if (propertyName.equals("coco"))
{
System.out.println("coco found at line : " + line);
break;
}
else if (propertyName.equals("titi"))
{
System.out.println("Titi found at line : " + line);
break;
}
line++;
}
Run Code Online (Sandbox Code Playgroud)
您认为我将在输出中拥有什么?
我会在你的答案后编辑问题.
谢谢.
好吧,所以我正在编写一个Java应用程序来导入一个csv文件,然后遍历结果,并将它们加载到一个数组中.我正在导入文件,因为它没有通过异常.我的问题是,当我尝试计算FileInputStream中的记录数时,我陷入了无限循环.这可能是什么问题.下面是代码:
这是我的类,有一个调用go()的Main方法:
public void go() {
pop = new PopularNames();
popGui = new PopularNamesGui();
String file = popGui.userInput("Enter the correct name of a file:");
pop.setInputStream(file);
pop.getNumberOfNames();
}
Run Code Online (Sandbox Code Playgroud)
这是类PopularNames(pop),在下面的方法中,我将inputStream var设置为新的FileINputStream.文件名由用户提供.
public void setInputStream(String aInputStream) {
try {
inputStream = new Scanner(new FileInputStream(aInputStream));
} catch (FileNotFoundException e) {
System.out.println("The file was not found.");
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
这是麻烦的方法.我只是循环遍历FileInputStream并计算记录数:
public void getNumberOfNames() {
while (this.inputStream.hasNext()) {
fileDataRows++;
}
}
Run Code Online (Sandbox Code Playgroud) 我的目标是从文件中读取和写入对象(使用文件I/O).这些对象是存储在动态中的userinput(name,int value,double value等..)arrayList.将arrayList在我的主类中声明.作为我程序的进一步改进,我想将这些数据保存在一个文件中,我已经创建了另一个类ReaderWriter来实现file I/O.
现在,我怎么能得到的参考arrayList在ReaderWriter上课吗?
我的申请很大.我将展示与我的问题相关的部分内容:
主要:
public static void main(String[] args) {
System.out.println("WELCOME TO OUR BANK!\n\n");
List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
}
Run Code Online (Sandbox Code Playgroud)
getter和setter的类:
public class BankAccount {
private String name;
private int accNum;
private double initiateAmount;
//constructor
public BankAccount() {
this.name = null;
this.accNum = 0;
this.initiateAmount = 0;
}
public void setName(String name) {
this.name = name;
.........................
.......................
............
Run Code Online (Sandbox Code Playgroud)
读写器:
public void writeToFile(){
try { …Run Code Online (Sandbox Code Playgroud) 我是编程新手,我需要帮助理解创建fileinputstream对象以读取文件的两种方法之间的区别.我在互联网上看过一些例子,有些人使用过第一张,有些人则用过第二张.我很困惑哪个更好,为什么?
FileInputStream file = new FileInputStream(new File(path));
FileInputStream file = new FileInputStream(path);
Run Code Online (Sandbox Code Playgroud) fileinputstream ×10
java ×9
android ×2
file-io ×2
arraylist ×1
directory ×1
encoding ×1
filereader ×1
iterator ×1
logfile ×1
netbeans ×1
properties ×1
uri ×1
xml ×1