Tag: fileinputstream

如何在java中将FileInputStream转换为字符串?

在我的java项目中,我将FileInputStream传递给一个函数,我需要转换(将类型转换为FileInputStream为字符串),如何做到这一点。 public static void checkfor(FileInputStream fis) { String a=new String; a=fis //how to do convert fileInputStream into string print string here }

FileChannel在assets文件夹中返回错误的文件文件大小

我试图使用FileInputStream从我的资产中的原始文件夹中读取File 。 这是我创建FileInputStream : AssetManager assetManager = getAssets(); AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); 之后,我试图从File读取数据,如下所示: FileChannel fileChannel = inputStream.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); IntBuffer intBuffer = mappedByteBuffer.asIntBuffer(); int[] array = new int[intBuffer.limit()]; intBuffer.get(array); inputStream.close(); fileChannel.close(); 但这不起作用。 由于某种原因, fileChannel.size()返回一个巨大的数字。 我有一个13字节长的测试文件,但fileChannel.size()返回1126498! 另外,如果我忽略大小并且只是开始读取返回的字节,则根本不匹配我的测试文件! 那么这里发生了什么? 有没有办法解决这个问题?

在创建FileInputStream对象时使用InputStream而不是FileInputStream有什么不同

这可能是一个愚蠢的,但我想知道背景操作的差异。 InputStream is = new FileInputStream(filepath); FileInputStream is = new FileInputStream(filepath); 上面两行代码之间的区别是什么,以及它们使用的场景。

从特定行开始读取文件

我有一个文件similaire:… The hotspot server JVM has specific code-path optimizations # which yield an approximate 10% gain over the client version. export CATALINA_OPTS=”$CATALINA_OPTS -server” #############HDK1001############# # Disable remote (distributed) garbage collection by Java clients # and remove ability for applications to call explicit GC collection export CATALINA_OPTS=”$CATALINA_OPTS -XX:+DisableExplicitGC” # Check for application specific parameters at startup […]

创建FileInputStream时出现java.io.FileNotFoundException

尝试打开FileInputStream以从.ser扩展名的文件加载Map时出错。 构造函数,我在其中创建新的File并调用从文件加载映射的方法: protected DriveatorImpl() { accounts = new ConcurrentHashMap(); db = new File(“database.ser”); // oddly this does not create a file if one does not exist loadDB(); } @SuppressWarnings(“unchecked”) private void loadDB() { try { fileIn = new FileInputStream(db); in = new ObjectInputStream(fileIn); accounts = (Map) in.readObject(); in.close(); fileIn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); […]

使用File对象初始化FileInputStream时获取FileNotFoundException

我试图使用File对象初始化FileInputStream对象。 我在线上收到FileNotFound错误 fis = new FileInputStream(file); 这很奇怪,因为我已经通过相同的方法打开这个文件多次执行正则表达式。 我的方法如下: private BufferedInputStream fileToBIS(File file){ FileInputStream fis = null; BufferedInputStream bis =null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bis; } java.io.FileNotFoundException:C:\ dev \ server \ tomcat6 \ webapps \ sample-site(访问被拒绝) at […]

FileDescriptor什么时候关闭?

我的应用需要执行以下操作: 打开FileInputStream ,获取底层的FileDescriptor (通过getFd() ) 基于上面的FileDescriptor创建新的FileInputStream对象 到目前为止,我只需要一个FileDescriptor ,所以我曾经通过在原始流上调用close()来关闭它(即在我调用的getFd()流上)。 我使用它是因为一些Android API方法有这样的参数。 现在我将同时拥有更多的FileInputStream对象, FileDescriptor何时关闭? (我猜:当所有FileInputStream对象都关闭?)

强制删除文件夹中的所有文件

我一直在使用一段特定的代码来删除文件夹中的文件,但事实certificate它很成问题,因为我可能忘了关闭一两个InputStream。 我的代码是如此之大,以至于我无法看到所有未关闭的输入流。 有没有办法删除文件是否有一个打开的InputStream? 这是我一直在使用的代码片段; File fin = new File(“C:/ABC Statements final/”); File[] finlist = fin.listFiles(); for (int n = 0; n < finlist.length; n++) { if (finlist[n].isFile()) { System.gc(); Thread.sleep(2000); finlist[n].delete(); } } 我编辑了代码。 这个版本有效。

如何解析从java中的文件读取的unicode

我写了一个包含以下内容的文本文件: \u0032\u0142o\u017Cy\u0142 然后我使用FileReader和BufferedReader来读取文件。 public static void main(String[] args) throws Exception{ FileInputStream fr = new FileInputStream(“README.TXT”); BufferedReader br = new BufferedReader(new InputStreamReader(fr,”UTF-8″)); String s=””; while((s=br.readLine())!=null){ System.out.println(s); } } 但输出是: \u0032\u0142o\u017Cy\u0142 。 我用的时候 System.out.println(“\u0032\u0142o\u017Cy\u0142”); 这些代码将被解析并以正确的forms显示。 如何更改我的代码,以便解析文件中的unicode并以正确的forms显示?

为什么FileInputStream中的read()返回一个整数?

此页面显示,当它想要指示没有更多字节要读取时,该方法可以返回-1。 但是一个字节的范围是-128到127,对吗? 因为返回一个字节,read()的返回类型是不是更有意义? 感谢您的时间。