我希望在c#中使用Stream阅读器分割文件 [英] I want Split file in twoparts using Stream reader in c#

查看:72
本文介绍了我希望在c#中使用Stream阅读器分割文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是代码没有正常工作,我希望使用Stream阅读器在twoparts中分割文件c#



使用System; 


公共类SplitFile
{
internal static int sizeOfFiles;

// JAVA到C#转换器警告:方法'throws'子句在.NET中不可用:
// ORIGINAL LINE:public static String [] splitFile(java.io.File f ,int noOfSlaves)抛出java.io.IOException
public static string [] splitFile(File f,int noOfSlaves)
{


BufferedInputStream bis = new BufferedInputStream( new FileInputStream(f));
FileOutputStream @out;
string name = f.Name;
int partCounter = 1;
int totalsize = bis.available();
Console.WriteLine(主文件总大小:+ totalsize);
// int sizeOfFiles = 1024 * 1024; // 1MB
sizeOfFiles = totalsize / noOfSlaves;
Console.WriteLine(sizeOfFiles:+ sizeOfFiles);
sbyte [] buffer = new sbyte [sizeOfFiles];
int tmp = 0;
int i = 0;
int j = 1;
string [] fileArray = new string [2];
while((tmp = bis.read(buffer))> 0)
{
fileArray [i] = f.Parent +\\+ name +。 + string.Format({0:D3},j);
文件newFile = new File(f.Parent +\\+ name +。+ string.Format({0:D3},partCounter ++));
i ++;
j ++;
newFile.createNewFile();
@out = new FileOutputStream(newFile);
@ out.write(buffer,0,tmp);
@ out.close();


}
return fileArray;

}

// JAVA到C#转换器警告:方法'throws'子句在.NET中不可用:
// ORIGINAL LINE:public static void main (String [] args)抛出java.io.IOException
public static void Main(string [] args)
{
splitFile(new file(c:\\data.txt ),2);
}

}









请帮帮我。

解决方案

请参阅 < u> Ron Beyer ,以及我对你最近提出的问题的回复:如何在c#中创建线程以及如何调用线程 [ ^ ]:

< blockquote class =FQ>

Ron 写道:

你真的需要停止下载Java代码,将其粘贴到C#编辑器中并期望它运行。这里有很多非常不同的概念(你没有从Thread派生出一个C#类),你应该先读一本书然后再深入了解线程等高级方法。

现在我明白他是什么了意味着。



这已成为一种真正的滥用。做你正在做的事情会适得其反,浪费时间充其量。



使用 System.IO.StreamReader的问题非常非常简单。您应该忘记Java代码并从其目标开始编写代码,而不是从现有代码编写代码。如果您解释这个目标并解释问题,我们将很乐意为您提供帮助。但是如果您只是简单地阅读原始文档,那么很可能您可以轻松地将其正确地理解: http://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx [ ^ ]。



-SA


my probelm is code is properly not working,I want Split file in twoparts using Stream reader in c#

using System;


public class SplitFile
{
  internal static int sizeOfFiles;

//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static String[] splitFile(java.io.File f, int noOfSlaves) throws java.io.IOException
	public static string[] splitFile(File f, int noOfSlaves)
	{


		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
		FileOutputStream @out;
		string name = f.Name;
		int partCounter = 1;
		int totalsize = bis.available();
		Console.WriteLine("Master File total size: " + totalsize);
		// int sizeOfFiles = 1024 * 1024;// 1MB
		sizeOfFiles = totalsize / noOfSlaves;
		Console.WriteLine("sizeOfFiles: " + sizeOfFiles);
		sbyte[] buffer = new sbyte[sizeOfFiles];
		int tmp = 0;
		int i = 0;
		int j = 1;
		string[] fileArray = new string[2];
		while ((tmp = bis.read(buffer)) > 0)
		{
			 fileArray[i] = f.Parent + "\\" + name + "." + string.Format("{0:D3}", j);
			File newFile = new File(f.Parent + "\\" + name + "." + string.Format("{0:D3}", partCounter++));
		   i++;
			j++;
			newFile.createNewFile();
			@out = new FileOutputStream(newFile);
			@out.write(buffer, 0, tmp);
			@out.close();


		}
		return fileArray;

	}

//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void main(String[] args) throws java.io.IOException
	public static void Main(string[] args)
	{
		splitFile(new File("c:\\data.txt"), 2);
	}

}





please help me.

解决方案

Please see the comment by Ron Beyer, as well as my reply to your recent question: how to create thread in c# and how to call thread[^]:

Ron wrote:

You really need to stop downloading Java code, pasting it into the C# editor and expecting it to run. There are a lot of concepts here that are very different (you don't derive a C# class from Thread), you should pick up a book and read it before diving into advanced methods like threading.

Now I understand what he meant.

This is becoming a real abuse. Doing what you are doing is counter-productive, the waste of time at best.

The problem of using System.IO.StreamReader is really, really simple. You should just forget your Java code and write the code starting from the goal of it, not from existing code. If you explain such goal and explain the problem, we will gladly help you. But it's very likely that you can easily put it right if you simply read original documentation with attention: http://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^].

—SA


这篇关于我希望在c#中使用Stream阅读器分割文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆