//******** File reading using scanner /** * Read given file line by line * @param m_strFileName File name * @throws Exception File not exist * @auther Amit 31/10/2011 */ void printFile(String m_strFileName) throws Exception { // Create an instance of File for data file. File objFile = new File(m_strFileName); if(objFile.exists() == false) { throw new Exception(m_strFileName + "File does not exist"); } // Reading and displaying file line by line Scanner objScanner = new Scanner(objFile); while(objScanner.hasNextLine() == true) { System.out.println(objScanner.nextLine()); } objScanner.close(); } //********** Using FileInputStream // Get the object of DataInputStream, passing FileInputStream DataInputStream in = new DataInputStream(new FileInputStream("anyfile.txt")); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the line on the console System.out.println (strLine); } in.close();
Monday, 31 October 2011
Java: File Read / Write
Labels:
Core Java
Friday, 7 October 2011
c#: XML serialization and deserialization, that is from class to xml and xml to class
Consider following as xml structure
it may contain three class, Employee, Employee_info, Branch etc
but really we don't need to care about these classes.
let's come to the actual coding
Amit +91-9033334254 1986-01-01 Male Single
ASI
IND
2000-01-10
- First you need to create class in which you can store xml data- For nested xml tag new class is created
- if you don't know how to create class for serialization then just search xsd.exe
in you c:\ drive, or download from internet. - Follow following command using xsd.exe in cmd
- C:\xsd.exe Employee.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\Employee.xsd'. - Above Command give u xml schema, later this schema will create class
- C:\xsd.exe Employee.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\Employee.cs'.
it may contain three class, Employee, Employee_info, Branch etc
but really we don't need to care about these classes.
let's come to the actual coding
// Creating Object of XML Serializer for Getting Data into Emplyee Object
XmlSerializer objXmlSerializer = new XmlSerializer(typeof(Employees));
TextReader objTextReader;
Stream objStream = null;
FileStream objFileStream = null;
// Generated class from xsd.exe
Employees objEmployees = null;
objFileStream = new FileStream("Employee.xml", FileMode.OpenOrCreate);
objTextReader = new StreamReader(objFileStream);
// Get the xml data into the objEmployees obj
objEmployees = (Employees)objXmlSerializer.Deserialize(objTextReader);
objFileStream.Close();
///
/// Display Employees Records on Console
///
public void DisplayEmployeeInfo(Employees Employee)
{
if (Employee == null )
{
Console.WriteLine(Defination.NoRecordFound);
return;
}
for (int nIndex = 0; nIndex < Employee.Length; nIndex++)
{
// Calculating emp age
TimeSpan objTimeSpan = DateTime.Now - DateTime.Parse(Employee[nIndex].BirthDate);
Console.Write("{0}\t{1}\t{2} {3} ", Employee[nIndex].Name, Employee[nIndex].Telephone, ((int)objTimeSpan.TotalDays / 365), Employee[nIndex].RelationshipStatus);
Console.Write("\t{0}\t{1}\t{2}", Employee[nIndex].Branch[0].Name, Employee[nIndex].Branch[0].CountryCode, Employee[nIndex].Branch[0].EstablishmentDate);
Console.WriteLine();
}
}
// for insert / update just take new emplyees obj and serialize it
Employees objNewEmployees = new Employees();
// fill the required info like u get it in Display() method
...
...
XmlSerializer serializer = new XmlSerializer(typeof(Employees));
TextWriter textWriter = new StreamWriter("New file.xml");
serializer.Serialize(textWriter, objNewEmployees);
textWriter.Close();
Labels:
C#
Thursday, 6 October 2011
Create Thumbnail for image using c#
// Setting Thumbnail size
int height = 600;
int width = 400;
// Getting desired image
Bitmap objImage = new Bitmap("--file path --");
// Showing image in picturebox, here pictureBox1 is pictureBox control
pictureBox1.Image = objImage.GetThumbnailImage(width, height,null, IntPtr.Zero);
Wednesday, 5 October 2011
How to merge any file with exe in VS2008 C#
If you want to merge any file then you can follow below procedure
ProjectName.Properties.Resources.Filename
If you want to merge dll file then you can follow below procedure
- Open Project Property [Menu/Project/Project Name Properties]
- Go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
- Add any file to your project
- Then from the toolbar above the tab-page, select Add Resource/Add Existing file and select your file or you can also drag and drop file from solution explorer.
ProjectName.Properties.Resources.Filename
If you want to merge dll file then you can follow below procedure
- download ILMerge
- put "
ILMerge.exe
" in your \WINNT directory - In VS.NET, right click project, Properties, Common Properties, Build Events
- In "Post-build Event Command Line" enter:
ilmerge /out:$(TargetDir)YOURAPPNAME.exe $(TargetPath) $(TargetDir)YOURDLLNAME.dll - Then compile the Release version (not the debug version).
- In your "
bin\Release
" directory, you will find aYOURAPPNAME.exe
which can be run on its own without the .dll.
Labels:
C#
Subscribe to:
Posts (Atom)