Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
my probelm is code is properly not working,I want Split file in twoparts using Stream reader in c#

HTML
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.
Posted

1 solution

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
 
Share this answer
 
Comments
Joezer BH 6-Nov-13 2:22am    
5ed!
Sergey Alexandrovich Kryukov 6-Nov-13 3:02am    
Thank you.
—SA
Ron Beyer 6-Nov-13 8:30am    
+5'd, whatever java->c# converter he is using is terrible. In situations like these its better to do a hand conversion rather than use a conversion tool. The code above doesn't (only) have a problem "splitting a file", it doesn't even compile. The first step to debugging is to get a program to at least run.

I can only imagine how many red-lines and error messages are in the editor windows on his project.
Sergey Alexandrovich Kryukov 6-Nov-13 9:03am    
Right... Thank you, Ron.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900