Tag Archives: file

C#

Read and write files with stream in C#

fsutil
Very basic example how to read and write files. The util class is as following:

using System.IO;
using System.Text;

namespace Project.Dao
{
    public class FileSystemUtils
    {
        public static string ReadFile(string filePath, Encoding encoding)
        {
            using (var reader = new StreamReader(filePath, encoding))
            {
                return reader.ReadToEnd();
            }
        }

        public static void WriteFile(string filePath, string content, Encoding encoding)
        {
            using (var writer = new StreamWriter(filePath, false, encoding))
            {
                writer.Write(content);
            }
        }
    }
}

KISS paradigm enjoys.

Delete files and folders with long names in Windows

four-file-folders
Using command line you can delete files and folders with long names:

  rmdir /S /Q <FOLDER_NAME_TO_DELETE>

Stream file download in a JSF managed bean

file_download

To output contents directly to browser in JSF 2.x use next method:

public static void downloadFile(String fileName, String content, String contentType) throws IOException {
  FacesContext fc = FacesContext.getCurrentInstance();
  ExternalContext ec = fc.getExternalContext();

  ec.responseReset();
  ec.setResponseContentType(contentType);
  ec.setResponseContentLength(content.length());
  ec.setResponseHeader("Content-Disposition", 
    "attachment; filename=\"" + fileName + "\"");

  OutputStream output = ec.getResponseOutputStream();
  output.write(content.getBytes());

  fc.responseComplete();
}

And example usage in action listener:

public void onSomeEvent(ActionEvent event) {
  try {
    StringBuilder result = new StringBuilder();
    result.append("ABC");
    downloadFile("file.csv", result.toString(), "text/csv");
  } catch (Exception e) {
    /* handle errors... */
  }
}

See also,
http://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean

Unlock file in Windows with ProcessExplorer

bb896653.processexplorer(en-us,MSDN.10)
It is often occurs locked files. How to unlock file from granted process? Very simple!
Just use ProcessExplorer from Windows Sysinternals.
Download here ProcessExplorer

To perform unlocking, click Ctrl+F and find you file. It’ll show to you the process locker.
Kill them and make fun!

Using SETLOCAL and ERRORLEVEL together in Batch

bat-file-icon
Interesting thing about using SETLOCAL and ERRORLEVEL together.
Consider two files:

first.bat

@echo off

echo %ERRORLEVEL% in first bat before setlocal

setlocal

echo Some actions with error finished here...

set ERRORLEVEL=1
echo %ERRORLEVEL% in first bat.

exit /b %ERRORLEVEL%

second.bat

@echo off

echo %ERRORLEVEL% in second bat before setlocal

setlocal

echo %ERRORLEVEL% in second bat after setlocal

Exec in series:

C:\first.bat && second.bat

You’ll see the next output:

0 in first bat before setlocal
Some actions with error finished here...
1 in first bat.
1 in second bat before setlocal
0 in second bat after setlocal

More info http://ss64.com/nt/setlocal.html

Set file and folder permissions in Linux

read-write-permission-attributes-linux
Convenient way to apply permissions in Linux is to save alias in .bashrc file:

Set all permissions for FTP:

alias setftpperm="chown -R _pure-ftpd:_pure-ftpd /usr/serv/ftp/anonymous && \
    chmod -R u+rwX,go+rX,go-w /usr/serv/ftp/anonymous && \
    chmod -R u+rwX,go+rX,go+w /usr/serv/ftp/anonymous/PUBLIC && \
    chmod -R u+rwX,go+rX,go+w /usr/serv/ftp/anonymous/TORRENTS"

Next is permissions for Apache:

alias setapacheperm="chown -R _pure-ftpd:daemon . && \
  chmod 666 . && chmod -R u+rwX,go+rX,go-w ."

alias setapachewperm="chown -R _pure-ftpd:daemon . && \
  chmod 666 . && chmod -R u+rwX,go+rX,go+w ."

Control Version System file structure proposals

cvs
Currently, i want to say a few words about CVS file structure.

If you have a number of projects, probably deal with the problem of right files organization.
In this case i mean projects are coupled with each other, i.e. dependencies presents.

Here is the default organization, in common way:

/-Project_1
     /-trunk
     /-branches
        /-branch_1
        /-branch_2
        /-branch_3
/-Project_2
     /-trunk
     /-branches
        /-branch_1
        /-branch_2
        /-branch_3
/-Project_3
     /-trunk
     /-branches
        /-branch_1
        /-branch_2
        /-branch_3

But this structure can be inconvenient to work with.
I propose use the next for coupled projects:

/-trunk
     /-Project_1
     /-Project_2
     /-Project_3
/-branches
     /-branch_1
          /-Project_1
          /-Project_2
          /-Project_3
     /-branch_2
          /-Project_1
          /-Project_2
          /-Project_3
     /-branch_3
          /-Project_1
          /-Project_2
          /-Project_3

This may seen some stupid, but you’ll have a pros in each time checkout process.
To get all you need you have to choose a branch and make checkout!
The file structure of all projects will be identical for each developer.